NO

Author Topic: fscanf problem  (Read 5394 times)

Hoborob

  • Guest
fscanf problem
« on: November 04, 2011, 08:59:41 PM »
I have entered a program from a textbook on C that has a line that uses the fscanf function. I have tested the program execution and teh program reaches this line but fails there. I have compiled the program on multiple systems and it fails on each one. The function is attempting to read 3 strings and a double number from a file stored on the hard drive to store that information in an array to be used by the program. I have looked at the line several times and and attempted to make changes to try to get it to woork. Unfortunately all attempts have met withteh same failure. the question I have is, is there a trick to testing this function to find out why it fails?

CommonTater

  • Guest
Re: fscanf problem
« Reply #1 on: November 04, 2011, 09:04:41 PM »
You should post the code... at least the parts that show us the scanf() problem.  It's pretty hard to diagnose or advise without seeing the actual problem.

If you put the text cursor on scanf() in your source and hit F1 you will get the help documentation for it, that may help you sort it out.

Hoborob

  • Guest
Re: fscanf problem
« Reply #2 on: November 07, 2011, 06:17:20 AM »
Here is the function where the failure occurs

/*   
 *  Gets data from a file to fill output argument
 *  Returns standard error code:  1 => successful input,  0 => error,   
 *                                negative EOF value => end of file
 */
int
fscan_unit(FILE   *filep,  /*  input - input file pointer        */
           unit_t *unitp)  /*  output - unit_t structure to fill */
{
      int status;
 
      status = fscanf(filep, "%s%s%s%lf", unitp->name, 
                                          unitp->abbrev, 
                                          unitp->class,
                                          &unitp->standard);
 
      if (status == 4)
            status = 1;
      else if (status != EOF)
            status = 0;
 
      return (status);

The line in blue is reached by the program during execution but the very next line never executes.

CommonTater

  • Guest
Re: fscanf problem
« Reply #3 on: November 07, 2011, 06:31:42 AM »
Ok... how does fscanf() know when one string ends and the next begins?  Usually by "white space"... spaces, tabs, newlines, etc. in the file.  It does pattern matching so you probably want something more like this...
Code: [Select]
fscanf(filep," %s %s %s %lf", unitp->name, unitp->addrev,unitp->class, unitp.standard);

Hoborob

  • Guest
Re: fscanf problem
« Reply #4 on: November 08, 2011, 08:24:18 PM »
Attempted this fix as well. The program still fails at this line.

I have attached the full source code which came from a textbook entitled "Problem Solving and Program Design in C" By Jeri R. Hanly and Elliot B. Koffman and the text file that is supposed to go with the program.

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: fscanf problem
« Reply #5 on: November 08, 2011, 08:32:59 PM »
Since the values are on a line of their own, you might need to separate the fields by \n.

Code: [Select]
fscanf(filep,"%s\n%s\n%s\n%lf\n", unitp->name, unitp->addrev,unitp->class, unitp.standard);
---
Stefan

Proud member of the UltraDefrag Development Team

Hoborob

  • Guest
Re: fscanf problem
« Reply #6 on: November 17, 2011, 01:04:33 AM »
The issue that I am having with this program is that I first not sure if the data file itself is set up correctly. Second I do not know any way to check to see if the file was opened correctly or not either. What I would like to know is how would I display the pointer to the file and what should I see when I do get it to display.

CommonTater

  • Guest
Re: fscanf problem
« Reply #7 on: November 17, 2011, 03:06:52 AM »
Checking the status of a file is pretty easy... fopen() returns null if the file open fails...
Code: [Select]
FILE *fptr = NULL;

fptr = fopen(filename,"r");
if (fptr ==  NULL)
  { printf("Well that certainly didn't work\n\n");
     exit(255); } // user assigned error level
Just make sure your program exits if the file doesn't open because continuing on will just bring a cascade of errors.

Also you probably should not put \n in your scanning functions... But if your file is not opening, the scanf() line you accented is the most likely place for it to fail on you...





« Last Edit: November 17, 2011, 03:09:26 AM by CommonTater »

Hoborob

  • Guest
Re: fscanf problem
« Reply #8 on: November 17, 2011, 04:21:43 AM »
Thank You CommonTater. Your input helped me track down that problem. the problem occurred in the fopen statement which the program provided no mechanism to checking if the file was opened properly or not. Once the correct filename was in the program the program worked just fine. I myself did not know how to check for a failure at that point.

CommonTater

  • Guest
Re: fscanf problem
« Reply #9 on: November 17, 2011, 04:33:37 AM »
Thank You CommonTater. Your input helped me track down that problem. the problem occurred in the fopen statement which the program provided no mechanism to checking if the file was opened properly or not. Once the correct filename was in the program the program worked just fine. I myself did not know how to check for a failure at that point.

Ok, now, just a small hint you can take forward with you... 

In the source editor, put your text cursor on the fopen() keyword and press F1...  The information you needed is there... In fact every function in the libraries is documented there, as are all keywords and a ton of other helpful stuff.

One thing Pelle gets real high points from me for is his help file... Pelles C is, without doubt,  the best documented compiler I've ever seen.