Thread: Reading info from a file...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    21

    Question Reading info from a file...

    I need some help. Either I'm wrong, or the compiler is broken. I got this program:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    main()
    {
    	FILE *fp;
    	char death[] = "bob.txt";
    	float l1=0, l2=0, l3=0, l4=0, l5=0;
    	if ( (fp = fopen(death, "r")) == NULL)
    	{
    	printf("error opening file.");
    	}
    	else
    	{
    	fscanf(fp, "%f, %f, %f, %f, %f", &l1, &l2, &l3, &l4, &l5);
    	printf("%f, %f, %f, %f, %f", l1, l2, l3, l4, l5);
    	fclose(fp);
    	getch();
    	}
    }
    The file bob.txt looks like this:

    Code:
    123.45     87.001
    100.02
    0.00456    1.0005
    But when the program executes, it outputs this:

    123.449997, 0.000000, 0.000000, 0.000000, 0.000000

    I have gone through the program over and over and can't figure out why it's not going to the next number in the file. Anyone have any ideas for me?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Consider the differences with this code.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       const char death[] = "bob.txt";
       FILE *fp = fopen(death, "r");
       if ( fp == NULL )
       {
          perror(death);
       }
       else
       {
          float l1=0, l2=0, l3=0, l4=0, l5=0;
          if ( fscanf(fp, "%f%f%f%f%f", &l1, &l2, &l3, &l4, &l5) == 5 )
          {
             printf("%f, %f, %f, %f, %f\n", l1, l2, l3, l4, l5);
          }
          else
          {
             puts("fscanf failed"); 
          }
          fclose(fp);
       }
       getchar();
       return 0;
    }
    
    /* my output
    123.449997, 87.000999, 100.019997, 0.004560, 1.000500
    */
    [EDIT]
    One of the main problems was your format specifier, which expected commas. Since none were found in the input file, fscanf failed. If you had been checking return values, this would have been immediately apparent.
    [/EDIT]
    Last edited by Dave_Sinkula; 07-02-2003 at 06:59 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    21
    The only real difference I can see is the "," in between the %f signals, which leads me to believe that they're fudging it up, since my program now works. Sheesh, it's always the little things.
    '
    Can you tell me what the commas in the fscanf function actually do and why they were messing it up?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Can you tell me what the commas in the fscanf function actually do and why they were messing it up?

    The second parameter to fscanf is a format string. The format string contains directives. If the directive is an ordinary character -- a "%," is not valid -- then the following applies:
    A directive that is an ordinary multibyte character is executed by reading the next characters of the stream. If any of those characters differ from the ones composing the directive, the directive fails and the differing and subsequent characters remain unread. Similarly, if end-of-file, an encoding error, or a read error prevents a character from being read, the directive fails.
    So yes, in your original code you were telling fscanf to fail if the values weren't comma-delineated.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM