fscanf help

This is a discussion on fscanf help within the C Programming forums, part of the General Programming Boards category; I've noticed when i use fscanf to advance through a file whenever i have a fscanf line it goes to ...

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    fscanf help

    I've noticed when i use fscanf to advance through a file whenever i have a fscanf line it goes to the next line in the file. What i want to do is:

    if theres 4 fields in the textfield :
    Code:
      while (fscanf(file, "%d %s %s %f", &temp->var1, temp->var2, 
    	  temp->var3, &temp->var4) == 4)
    { 
            if (fscanf(file, "%d %s %s %f", &temp->var1, temp->var2, 
    	  temp->var3, &temp->var4) != 4)
            {
                  exit(0); // exit here since it didn't meet our crieteria.
            }   
    
         // all went OK do something with the file.
    }
    
    
    any idea on why it skips some lines and how to exactly check if fscanf has returned 4 or not without going to a next line?

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Use a combination of fgets and sscanf.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    hi, with sscanf i had one problem which is why i didn't use it:

    Code:
      if ( sscanf(buffer, "%d %s %s %f%n", &temp->var1, temp->var2, 
    	  temp->var3, &temp->var4)  == 4)
    if theres an invalid float in the text field like:

    123 field2 field3 23/23

    it proceeds to round it off to 23.00 ?? how do i detect this

  4. #4
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    for example:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct salesRecord
    {
       float budget;
    };
    
    typedef struct salesRecord saleNode;
    
    int main ()
    {
      char sentence []="23/33";
      
      saleNode *test = (saleNode *) malloc(sizeof(saleNode));
    
      sscanf (sentence,"%f", &test->budget);
      
      printf("%f", test->budget);
      
      return 0;
    }
    I want to detect that 23/33 is an invalid float and exit. Any ideas?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    just read this so sorry if what im thinking of is incorrect or irrelevant..

    can you use a loop to go through the characters in the input and if that character isnt '.' and isdigit(character) != 0 (and '.' appears at maximum one time) then it will be a float..?

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You want to detect fractions?

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i think he just wants to detect if this field is not a float, and 23/33 not being one. of course you can convert it but i think he just wants to disregard that entry..maybe im wrong

  8. #8
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    yes that would work, but im sure there is a way to do it with the return values of fscanf.

    for example this partially works:

    Code:
     while (!feof(ifp)) 
       {
           //initialize temp node..
      
          if ( fscanf(ifp, "%d %s %s %f%n", &temp->var1, temp->var2, 
             temp->var3, &temp->var4) == 4)
          {
                /* Everything went ok, do something */
          }
          else {
               exit(0);
           }
       }
    the problem is the code always falls into the "else" part forcing it to exit ?? :|

  9. #9
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Quote Originally Posted by nadroj
    i think he just wants to detect if this field is not a float, and 23/33 not being one. of course you can convert it but i think he just wants to disregard that entry..maybe im wrong
    yes that's exactly what i'm trying to do. So if theres a corrupt/invalid float i don't want to process the file and exit

  10. #10
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You want to eliminate the floats that have a non-finite decimal expansion ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  11. #11
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    this is close but i can't specify an "IF" to exit. If there's an invalid float encountered it just runs into an endless loop. How do i check if it's not 4??

    I ccan't repeat
    Code:
    fscanf(fp, "%d %s %s %f", &temp->empNo, temp->lastName, 
             temp->firstName, &temp->hourlyRate) == 4
    since that would scan to the next line and i would miss a line...

    Code:
    
      while (!feof(fp)) 
       {
             //initalize temp node
      
          while ( fscanf(fp, "%d %s %s %f", &temp->empNo, temp->lastName, 
             temp->firstName, &temp->hourlyRate) == 4)
          {
          	// printf("DONE");
          }
       }
    You want to eliminate the floats that have a non-finite decimal expansion ?
    I want to check if the file given is in the correct format in terms of INT, STRING, STRING, FLOAT otherwise i want the program to simply exit.

  12. #12
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    nevermind. I got it i think afte the while i just checked if its end of the file or not and exited.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    maybe read all of the fields in as character arrays, doing the test for valid fields on the variables that it saved the data to, and using your if instead of while, ie:
    Code:
     while (!feof(ifp)) 
       {
           //initialize temp node..
      
          if ( fscanf(ifp, "%s %s %s %s%s", &temp->var1, temp->var2, 
             temp->var3, &temp->var4) == 4) //of course youd have to change the var2,var3,etc, types to c-strings 
    //(or create a 2nd kind of struct with this format)
          {
               // use functions here to make sure each variable is correct, ie
               // the loop and isdigit as i mentioned earlier on the float (var4?)
                /* Everything went ok, do something */
          }
          else {
               exit(0);
           }
       }
    hope that helps

    edit:: whoops too late.. guess you found easier solution anyways!

  14. #14
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Not to burst your bubble or anything, but you probably don't want to use foef() to control a loop.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

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-29-2008, 12:11 AM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  3. fscanf causing a crash
    By dougwilliams in forum C Programming
    Replies: 6
    Last Post: 11-18-2007, 04:52 PM
  4. fscanf problem in C
    By kepler in forum C Programming
    Replies: 6
    Last Post: 09-30-2003, 07:24 AM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 02:22 PM

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