Solved

fopen with fscanf in C

Posted on 2007-03-19
4
1,541 Views
Last Modified: 2012-08-13
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
      
0
Comment
Question by:4eyesgirl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 18754312
You can do as in the getch loop

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
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18757262
>> ***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
0
 

Author Comment

by:4eyesgirl
ID: 18787558

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,email
Please help!
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 18788911
Try something like this :

  char *buff = (char*) malloc(MAX_LEN * 4 + 5);

  /* ... */

  while (fgets(buff, MAX_LEN * 4 + 5, fp)) {
    if (sscanf(buff, "%[^,],%[^,],%[^,],%[^,\n]\n", firstName, lastName, DOB, email) != 4) continue;

    fprintf(stdout, "firstName : %s\n", firstName);
    fprintf(stdout, "lastName : %s\n", lastName);
    fprintf(stdout, "DOB : %s\n", DOB);
    fprintf(stdout, "email : %s\n", email);
    fprintf(stdout, "\n");
  }

The (MAX_LEN * 4 + 5) is the maximum allowed length of a line based on MAX_LEN.

Note that I also filtered out invalid lines by checking the return value of sscanf.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xamarin c# deserialize Json containing nested object 2 148
smtp c source code 7 108
gdb doesn't stop on breakpoint 2 114
Resolve Dependency Issues 4 86
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question