本文整理汇总了C++中safe_fopen函数的典型用法代码示例。如果您正苦于以下问题:C++ safe_fopen函数的具体用法?C++ safe_fopen怎么用?C++ safe_fopen使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了safe_fopen函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: readCoord
void readCoord(const char* filename, SOP& sop){
//printf("Reading coordinates from '%s'.\n", filename);
int is_xyz = 0;
char buffer[BUF_SIZE+1];
FILE* file = safe_fopen(filename, "r");
if(file == NULL){
DIE("ERROR: Coordinates file %s can not be found.\n", filename);
}
if (!strcmp(filename + strlen(filename)-4, ".xyz")) {
is_xyz = 1;
safe_fgets(buffer, BUF_SIZE, file);
safe_fgets(buffer, BUF_SIZE, file);
}
int index = 0;
while(fgets(buffer, BUF_SIZE, file) != NULL){
if(is_xyz){
char *pch = strtok(buffer, " \t\r\n");
pch = strtok(NULL, " \t\r\n");
sop.aminos[index].x = atof(pch);
pch = strtok(NULL, " \t\r\n");
sop.aminos[index].y = atof(pch);
pch = strtok(NULL, " \t\r\n");
sop.aminos[index].z = atof(pch);
index++;
}
if(!is_xyz && strncmp(buffer, "ATOM", 4) == 0){
PDBAtom tmp;
tmp.parse(buffer);
sop.aminos[index].x = tmp.x;
sop.aminos[index].y = tmp.y;
sop.aminos[index].z = tmp.z;
#ifdef DEBUG
printf("%d: %f, %f, %f\n", index, sop.aminos[index].x, sop.aminos[index].y, sop.aminos[index].z);
#endif
index++;
}
}
fclose(file);
if(index == 0){
DIE("Can't read pdb file.\n");
}
if(index != sop.aminos.size()){
DIE("Read coordinates for %d beads, yet topology has %zu beads.\n", index, sop.aminos.size());
}
//printf("Done reading coordinates.\n");
}
示例2: parse_bam_list
void parse_bam_list( parameters** params)
{
FILE* bam_list;
char next_path[1024];
int i;
bam_list = safe_fopen( ( *params)->bam_list_path, "r");
i = 0;
while( fscanf( bam_list, "%s\n", next_path) == 1)
{
set_str( &( ( *params)->bam_file_list)[i], next_path);
i = i + 1;
}
fclose( bam_list);
}
示例3: LogHashChange
void LogHashChange(const char *file, FileState status, char *msg, const Promise *pp)
{
FILE *fp;
char fname[CF_BUFSIZE];
time_t now = time(NULL);
mode_t perm = 0600;
static char prevFile[CF_MAXVARSIZE] = ""; /* GLOBAL_X */
// we might get called twice..
if (strcmp(file, prevFile) == 0)
{
return;
}
strlcpy(prevFile, file, CF_MAXVARSIZE);
/* This is inefficient but we don't want to lose any data */
snprintf(fname, CF_BUFSIZE, "%s/state/%s", CFWORKDIR, CF_FILECHANGE_NEW);
MapName(fname);
#ifndef __MINGW32__
struct stat sb;
if (stat(fname, &sb) != -1)
{
if (sb.st_mode & (S_IWGRP | S_IWOTH))
{
Log(LOG_LEVEL_ERR, "File '%s' (owner %ju) is writable by others (security exception)", fname, (uintmax_t)sb.st_uid);
}
}
#endif /* !__MINGW32__ */
if ((fp = safe_fopen(fname, "a")) == NULL)
{
Log(LOG_LEVEL_ERR, "Could not write to the hash change log. (fopen: %s)", GetErrorStr());
return;
}
const char *handle = PromiseID(pp);
fprintf(fp, "%ld,%s,%s,%c,%s\n", (long) now, handle, file, FileStateToChar(status), msg);
fclose(fp);
safe_chmod(fname, perm);
}
示例4: mutt_help
void mutt_help (int menu)
{
char t[_POSIX_PATH_MAX];
char buf[SHORT_STRING];
const char *desc;
FILE *f;
const struct binding_t *funcs;
mutt_mktemp (t, sizeof (t));
funcs = km_get_table (menu);
desc = mutt_getnamebyvalue (menu, Menus);
if (!desc)
desc = _("<UNKNOWN>");
do {
if ((f = safe_fopen (t, "w")) == NULL)
{
mutt_perror (t);
return;
}
dump_menu (f, menu);
if (menu != MENU_EDITOR && menu != MENU_PAGER)
{
fputs (_("\nGeneric bindings:\n\n"), f);
dump_menu (f, MENU_GENERIC);
}
fputs (_("\nUnbound functions:\n\n"), f);
if (funcs)
dump_unbound (f, funcs, Keymaps[menu], NULL);
if (menu != MENU_PAGER)
dump_unbound (f, OpGeneric, Keymaps[MENU_GENERIC], Keymaps[menu]);
safe_fclose (&f);
snprintf (buf, sizeof (buf), _("Help for %s"), desc);
}
while
(mutt_do_pager (buf, t,
M_PAGER_RETWINCH | M_PAGER_MARKER | M_PAGER_NSKIP | M_PAGER_NOWRAP,
NULL)
== OP_REFORMAT_WINCH);
}
示例5: ParserStateReset
Policy *ParserParseFile(AgentType agent_type, const char *path, unsigned int warnings, unsigned int warnings_error)
{
ParserStateReset(&P, false);
P.agent_type = agent_type;
P.policy = PolicyNew();
P.warnings = warnings;
P.warnings_error = warnings_error;
strncpy(P.filename, path, CF_MAXVARSIZE);
yyin = safe_fopen(path, "rt");
if (yyin == NULL)
{
Log(LOG_LEVEL_ERR, "While opening file '%s' for parsing. (fopen: %s)", path, GetErrorStr());
exit(EXIT_FAILURE);
}
while (!feof(yyin))
{
yyparse();
if (ferror(yyin))
{
perror("cfengine");
exit(EXIT_FAILURE);
}
}
fclose(yyin);
if (P.error_count > 0)
{
PolicyDestroy(P.policy);
ParserStateReset(&P, true);
ParserStateClean(&P);
return NULL;
}
Policy *policy = P.policy;
ParserStateReset(&P, false);
ParserStateClean(&P);
return policy;
}
示例6: mutt_bcache_get
FILE* mutt_bcache_get(body_cache_t *bcache, const char *id)
{
char path[_POSIX_PATH_MAX];
FILE* fp = NULL;
if (!id || !*id || !bcache)
return NULL;
path[0] = '\0';
safe_strncat (path, sizeof (path), bcache->path, bcache->pathlen);
safe_strncat (path, sizeof (path), id, mutt_strlen (id));
fp = safe_fopen (path, "r");
dprint (3, (debugfile, "bcache: get: '%s': %s\n", path, fp == NULL ? "no" : "yes"));
return fp;
}
示例7: main
int main(){
char *file_name = safe_malloc(sizeof(char) * MAX_FILENAME);
char *chunk = safe_malloc(sizeof(char)*READSIZE);
//read file name from the master process and call map function
//on that file, return the result to the master process
while(safe_read(STDIN_FILENO, file_name, MAX_FILENAME) > 0){
FILE *file = safe_fopen(file_name, "r");
while(fread(chunk, READSIZE-1, 1, file) > 0){
//make sure the chunk is null-terminated
chunk[READSIZE-1] = '\0';
map(chunk, STDOUT_FILENO);
}
safe_fclose(file);
}
free(file_name);
free(chunk);
return 0;
}
示例8: EvalFileWrite
void EvalFileWrite(const void *mode, qCtx *ctx, qStr *out, qArgAry *args)
{
CStr path = ctx->ParseStr((*args)[0]);
FILE *fp;
int err = 0;
#ifndef WIN32
CStr perm_str = ctx->ParseStr((*args)[2]);
int perms = strtol(perm_str.SafeP(),(char **)NULL, 0);
mode_t prev_perms;
if (perms) {
prev_perms = umask((mode_t)~perms);
printf("umask: %d (%d)\n", perms, prev_perms);
}
try {
#endif
if (path.IsEmpty())
return;
fp = safe_fopen(ctx, path, (const char *) mode);
if (!fp) err = GetLastError();
#ifndef WIN32
} catch (qCtxEx ex) {
if (perms) umask(prev_perms);
throw ex;
}
if (perms) umask(prev_perms);
#endif
if (!fp) {
ctx->ThrowF(out, 601, "Failed to open file for writing. %y", err);
return;
}
qStrFileO fo(fp, true);
qCtxTmp sub(ctx);
sub.MapObj(fp, EvalFileFlush,"flush");
sub.Parse(args->GetAt(1), &fo);
}
示例9: PrintFile
static bool PrintFile(const char *filename, size_t max_lines)
{
if (!filename)
{
Log(LOG_LEVEL_VERBOSE, "Printfile promise was incomplete, with no filename.");
return false;
}
FILE *fp = safe_fopen(filename, "r");
if (!fp)
{
Log(LOG_LEVEL_ERR, "Printing of file '%s' was not possible. (fopen: %s)", filename, GetErrorStr());
return false;
}
size_t line_size = CF_BUFSIZE;
char *line = xmalloc(line_size);
for (size_t i = 0; i < max_lines; i++)
{
if (CfReadLine(&line, &line_size, fp) == -1)
{
if (ferror(fp))
{
Log(LOG_LEVEL_ERR, "Failed to read line from stream, (getline: %s)", GetErrorStr());
free(line);
return false;
}
else
{
break;
}
}
ReportToLog(line);
}
fclose(fp);
free(line);
return true;
}
示例10: _mutt_rename_file
int _mutt_rename_file (char *oldfile, char *newfile, int overwrite)
{
FILE *ofp, *nfp;
if (access (oldfile, F_OK) != 0)
return 1;
if (!overwrite && access (newfile, F_OK) == 0)
return 2;
if ((ofp = fopen (oldfile, "r")) == NULL)
return 3;
if ((nfp = safe_fopen (newfile, "w")) == NULL) {
fclose (ofp);
return 3;
}
mutt_copy_stream (ofp, nfp);
fclose (nfp);
fclose (ofp);
mutt_unlink (oldfile);
return 0;
}
示例11: PrintFile
static void PrintFile(EvalContext *ctx, Attributes a, Promise *pp)
{
FILE *fp;
char buffer[CF_BUFSIZE];
int lines = 0;
if (a.report.filename == NULL)
{
Log(LOG_LEVEL_VERBOSE, "Printfile promise was incomplete, with no filename.");
return;
}
if ((fp = safe_fopen(a.report.filename, "r")) == NULL)
{
cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_INTERRUPTED, pp, a, "Printing of file '%s' was not possible. (fopen: %s)", a.report.filename, GetErrorStr());
return;
}
while ((lines < a.report.numlines))
{
if (fgets(buffer, sizeof(buffer), fp) == NULL)
{
if (ferror(fp))
{
UnexpectedError("Failed to read line from stream");
break;
}
else /* feof */
{
break;
}
}
Log(LOG_LEVEL_ERR, "R: %s", buffer);
lines++;
}
fclose(fp);
}
示例12: mutt_get_tmp_attachment
int mutt_get_tmp_attachment (BODY *a)
{
char type[STRING];
char tempfile[_POSIX_PATH_MAX];
rfc1524_entry *entry = rfc1524_new_entry();
FILE *fpin = NULL, *fpout = NULL;
struct stat st;
if(a->unlink)
return 0;
snprintf(type, sizeof(type), "%s/%s", TYPE(a), a->subtype);
rfc1524_mailcap_lookup(a, type, entry, 0);
rfc1524_expand_filename(entry->nametemplate, a->filename,
tempfile, sizeof(tempfile));
rfc1524_free_entry(&entry);
if(stat(a->filename, &st) == -1)
return -1;
if((fpin = fopen(a->filename, "r")) && (fpout = safe_fopen(tempfile, "w"))) /* __FOPEN_CHECKED__ */
{
mutt_copy_stream (fpin, fpout);
mutt_str_replace (&a->filename, tempfile);
a->unlink = 1;
if(a->stamp >= st.st_mtime)
mutt_stamp_attachment(a);
}
else
mutt_perror(fpin ? tempfile : a->filename);
if(fpin) safe_fclose (&fpin);
if(fpout) safe_fclose (&fpout);
return a->unlink ? 0 : -1;
}
示例13: initialise_3d_displacement_maps
void initialise_3d_displacement_maps ( char *filename )
{
FILE
*fp;
int
count;
fp = safe_fopen ( filename, "rb" );
fread ( &number_of_displacement_maps, sizeof ( int ), 1, fp );
if ( number_of_displacement_maps )
{
displacement_maps = safe_malloc ( sizeof ( displacement_map ) * number_of_displacement_maps );
for ( count = 0; count < number_of_displacement_maps; count++ )
{
int
width,
height;
fread ( &width, sizeof ( int ), 1, fp );
fread ( &height, sizeof ( int ), 1, fp );
displacement_maps[count].width = width;
displacement_maps[count].height = height;
displacement_maps[count].data = safe_malloc ( width * height );
fread ( displacement_maps[count].data, width, height, fp );
}
}
}
示例14: printf
void PDB::read(const char* filename){
printf("Reading %s.\n", filename);
this->is_xyz = (!strcmp(filename + strlen(filename)-4, ".xyz"));
if (this->is_xyz) {
this->readXYZ(filename);
return;
}
char buffer[BUF_SIZE];
FILE* file = safe_fopen(filename, "r");
while(fgets(buffer, BUF_SIZE, file) != NULL){
if(strncmp(buffer,"SSBOND",6) == 0){
this->ssbonds.push_back(PDBSSBond(buffer));
}
if(strncmp(buffer, "ATOM", 4) == 0){
this->atoms.push_back(PDBAtom(buffer));
}
}
printf("Found %zu atoms and %zu SS-bonds.\n", this->atoms.size(), this->ssbonds.size());
printf("Done reading '%s'.\n", filename);
fclose(file);
}
示例15: mutt_decode_save_attachment
/* returns 0 on success, -1 on error */
int mutt_decode_save_attachment (FILE *fp, BODY *m, char *path,
int displaying, int flags)
{
STATE s;
unsigned int saved_encoding = 0;
BODY *saved_parts = NULL;
HEADER *saved_hdr = NULL;
memset (&s, 0, sizeof (s));
s.flags = displaying;
if (flags == M_SAVE_APPEND)
s.fpout = fopen (path, "a");
else if (flags == M_SAVE_OVERWRITE)
s.fpout = fopen (path, "w"); /* __FOPEN_CHECKED__ */
else
s.fpout = safe_fopen (path, "w");
if (s.fpout == NULL)
{
mutt_perror ("fopen");
return (-1);
}
if (fp == NULL)
{
/* When called from the compose menu, the attachment isn't parsed,
* so we need to do it here. */
struct stat st;
if (stat (m->filename, &st) == -1)
{
mutt_perror ("stat");
safe_fclose (&s.fpout);
return (-1);
}
if ((s.fpin = fopen (m->filename, "r")) == NULL)
{
mutt_perror ("fopen");
return (-1);
}
saved_encoding = m->encoding;
if (!is_multipart (m))
m->encoding = ENC8BIT;
m->length = st.st_size;
m->offset = 0;
saved_parts = m->parts;
saved_hdr = m->hdr;
mutt_parse_part (s.fpin, m);
if (m->noconv || is_multipart (m))
s.flags |= M_CHARCONV;
}
else
{
s.fpin = fp;
s.flags |= M_CHARCONV;
}
mutt_body_handler (m, &s);
safe_fclose (&s.fpout);
if (fp == NULL)
{
m->length = 0;
m->encoding = saved_encoding;
if (saved_parts)
{
mutt_free_header (&m->hdr);
m->parts = saved_parts;
m->hdr = saved_hdr;
}
safe_fclose (&s.fpin);
}
return (0);
}