fscanf() help!!

This is a discussion on fscanf() help!! within the C Programming forums, part of the General Programming Boards category; Hey there~ Im kind of new in C programming and i need help on reading files from a text file ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    15

    fscanf() help!!

    Hey there~ Im kind of new in C programming and i need help on reading files from a text file using fscanf()..The data from the text file is seperated by ":"..Here's the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      FILE* in;
      int id;
      char title[100];
      char author[100];
      int year;
      double price;
      int count;
    
      if((in = fopen("books.txt", "r")) == NULL)
      {
        printf("Error!!");
        exit(-1);
      }
    
      fscanf(in,"%d:%s:%s:%d:%lf:%d",&id,title,author,&year,&price,&count);
     
      while(! feof(in))
      {
        printf("%d:%s:%s:%d:%lf:%d\n",id,title,author,year,price,count);
        fscanf(in,"%d:%s:%s:%d:%lf:%d",&id,title,author,&year,&price,&count);
      }
    
      fclose(in);
    
      return EXIT_SUCCESS;
    }
    The file "books.txt" which im supposed to read in is:
    Code:
    4:Web Design In A Nutshell:J. Niederst:1999:54.95:5
    1:A Book on C:A. Kelly:1997:89.95:7
    There are two problems in my program...First of all, fscanf tends to skip white spaces....Second of all,there is an infinite loop in my while loop!! Or are there any other ways to read data from a text file seperated by a ":"?? Hope you all can help me out in this...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    1. while(! feof(in))
    See the FAQ - this is wrong.

    2. The problem with using any scanf function is that if it matches unexpected data, then the stream is NOT advanced.
    So your "%d" at the start of the convertion will halt forever if the next char in the file is say a letter.

    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, in ) != NULL ) {
      if ( sscanf(buff,"%d:%s:%s:%d:%lf:%d",&id,title,author,&year,&price,&count) == 6 ) {
        printf("%d:%s:%s:%d:%lf:%d\n",id,title,author,year,price,count);
      } else {
        printf( "Error in line %s\n", buff );
      }
    }
    > First of all, fscanf tends to skip white spaces
    %s stops at white space. If you're reading : delimited files, then I suggest you replace each "%s" with "%[^:]"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    Thanks for the reply!! So im supposed to replace all "%s" with "%[^:]" to avoid skipping the white spaces...But i dont know how to replace it...Below is how i replace all the "%s"..But it doesnt seem to be working...

    Code:
        if ( sscanf(buff,"%d:%[^:]:%[^:]:%d:%lf:%d",&id,title,author,&year,&price,&count) == 6)
        {
          printf("%d:%[^:]:%[^:]:%d:%lf:%d\n",id,title,author,year,price,count);
        }

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Use %[ in the sscanf, but not the printf. You may as well learn to limit string input by specifying width.
    Code:
             if ( sscanf(buff, "%d:%99[^:]:%99[^:]:%d:%lf:%d",
                         &id, title, author, &year, &price, &count) == 6 )
             {
                printf("%d:%s:%s:%d:%f:%d\n",id,title,author,year,price,count);
             }
    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.*

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    Thanks a lot for helping me out Salem and Dave~!! It works perfectly now~!! Dont know what i will do without both your help~!! ^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. fscanf help
    By Axel in forum C Programming
    Replies: 13
    Last Post: 10-22-2006, 10:44 PM
  4. Using fscanf with a structure
    By daluu in forum C Programming
    Replies: 10
    Last Post: 10-11-2004, 01:32 PM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 01:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21