4eyesgirl
asked on
fopen with fscanf in C
I want to read some data from a file into a data structure, how can I use the scanf function and do error checking?
For example -
FILE *fp;
fp = fopen(filename, "r");
Entry entry = new Entry();
if (fp == NULL)
{
fprintf(stderr, "Error: Unable to open file %s\n\n", string);
return FALSE;
}
//while( (ch=getc(fptr)) != EOF )
***Normally I would try to get each character and see if I reach the end of file, but if I want to use fscanf, how should I detect the eof?***
***Also, what about if there is error in the input file? For example, If I am expecting a pattern like john, smith, 1/1/2000, john.smith@email.com
***I would like to know how I can detect fields missing or corrupted error with scanf?
fscanf(fp, "%s,%s,%s,%s", entry.firstName, entry.lastName,
entry.DOB, entry.email);
Look forward to hear from you
For example -
FILE *fp;
fp = fopen(filename, "r");
Entry entry = new Entry();
if (fp == NULL)
{
fprintf(stderr, "Error: Unable to open file %s\n\n", string);
return FALSE;
}
//while( (ch=getc(fptr)) != EOF )
***Normally I would try to get each character and see if I reach the end of file, but if I want to use fscanf, how should I detect the eof?***
***Also, what about if there is error in the input file? For example, If I am expecting a pattern like john, smith, 1/1/2000, john.smith@email.com
***I would like to know how I can detect fields missing or corrupted error with scanf?
fscanf(fp, "%s,%s,%s,%s", entry.firstName, entry.lastName,
entry.DOB, entry.email);
Look forward to hear from you
>> ***I would like to know how I can detect fields missing or corrupted error with scanf?
The return value of fscanf() contains the number of successfully read fields. So, if you're expecting 3 fields, and fscanf() returns 2, then you know that there's a problem in the record.
Similarly, when the end of the file is met before fscanf could read any data, EOF is returned.
More information :
http://www.cplusplus.com/reference/clibrary/cstdio/fscanf.html
The return value of fscanf() contains the number of successfully read fields. So, if you're expecting 3 fields, and fscanf() returns 2, then you know that there's a problem in the record.
Similarly, when the end of the file is met before fscanf could read any data, EOF is returned.
More information :
http://www.cplusplus.com/reference/clibrary/cstdio/fscanf.html
ASKER
char* firstName = (char*) malloc(MAX_LEN);
char* lastName = (char*) malloc(MAX_LEN);
char* DOB = (char*) malloc(MAX_LEN);
char* email= (char*) malloc(MAX_LEN);
when I try to use
while( (fscanf(fp, "%s,%s,%s,%s", firstName, lastName,
DOB, email) != EOF)) {
//....does not work
// firstName -> read the entire line. Doesn't looks like fscanf separate with then input format that I provided.
}
however, using space works, why?
while( (fscanf(fp, "%s %s %s %s", firstName, lastName,
DOB, email) != EOF)) {
//....
}
I don't want to change my file format. My file format is firstname,lastname,dob,ema
Please help!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
while((fscanf(fp, fmt_string, args...) != EOF)
However you must check the return value, because on read failure (for whatever reason EOF is returned), however it might be a different problem so you have to check errno too if you want to be on the safe side.
Regards
Friedrich