J
jespersh
I have a problem concerning fscanf: I which to save an array of
structures to a file, and to boil things down let's say I save the
'type' member (which is a char) as:
void save(struct part *ptr, const char *file, int npart){
FILE *fout;
int n;
fout = fopen(file, "w");
/* some check */
for ( n=0; n<npart; n++ )
fprintf(fout, "%c\n", ptr[n].type);
fclose(fout);
}
(Here the array is 'npart' long.) Now, I wish to load the data through
the following function:
void load(struct part *ptr, const char *file, int npart){
FILE *fin;
int n;
fin = fopen(file, "r");
/* some check */
for ( n=0; n<npart; n++ )
fscanf(fin, "%c", &ptr[n].type);
fclose(fin);
}
This will lead to very strange behavior. As I understand fscanf skips
\n, space etc, but it seems from the output as if it includes the
newline character twice.
Am I missing the definition of fscanf?
Jesper
structures to a file, and to boil things down let's say I save the
'type' member (which is a char) as:
void save(struct part *ptr, const char *file, int npart){
FILE *fout;
int n;
fout = fopen(file, "w");
/* some check */
for ( n=0; n<npart; n++ )
fprintf(fout, "%c\n", ptr[n].type);
fclose(fout);
}
(Here the array is 'npart' long.) Now, I wish to load the data through
the following function:
void load(struct part *ptr, const char *file, int npart){
FILE *fin;
int n;
fin = fopen(file, "r");
/* some check */
for ( n=0; n<npart; n++ )
fscanf(fin, "%c", &ptr[n].type);
fclose(fin);
}
This will lead to very strange behavior. As I understand fscanf skips
\n, space etc, but it seems from the output as if it includes the
newline character twice.
Am I missing the definition of fscanf?
Jesper