Thread: Scan data from file

  1. #1
    Unregistered
    Guest

    Question Scan data from file

    Now I would like to open one data file, which utilizes the ';' to start commend. I want to scan all data before ;, which use a blank to separate each other.
    The following code I just can scan the first data, how can I modify it to scan the following data before ';'.

    Thanx a lot.

    #include <stdio.h>
    #include <string.h>

    int main ( )
    {
    FILE *fp = fopen( "input.txt", "r" );
    char buff[BUFSIZ],c;
    int i;
    float data[100];

    while ( fgets(buff,BUFSIZ,fp) != NULL )
    {
    float var;
    char *comment = strchr( buff, ';' );

    if ( comment != NULL ) *comment = '\0'; //blow away the comment
    if ( sscanf( buff, "%f", &var ) == 1 )
    printf( "Data=%f\n", var );
    }
    system("PAUSE");
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    14
    like this
    PHP Code:
    #include     <stdio.h>
    int main()
        {
        
    FILE*fp=fopen("input.txt","r+");
        
    float data[100];
        
    char test;
        
    int i;
        
        while(!
    feof(fp)&&test=getc(fp)!=';')
            {
            
    ungetc(test,fp);
            
    fscanf(fp,"%f",&data[i]);
            
    i++;
            }
        } 

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You shouldn't loop in this manner:
    Code:
     
    while(!feof(fp)&&test=getc(fp)!=';')
            {
            ungetc(test,fp);
            fscanf(fp,"%f",&data[i]);
            i++;
            }
    Using feof() in the way isn't guaranteed to produce the desired results. You should check the return code from getc() against EOF to see if you're at the end of a file. To do this, make use the variable you are assigning the return value to is an int.
    Code:
    int c;
    while ((c = getc(fp)) != EOF)
    {
       /* do stuff */
    }
    Also, better check the return code from fscanf() to ensure it did actually read a float into the output variable.

    The int i needs initialising before you use it too, it's starting value undefined.
    Last edited by Hammer; 05-12-2002 at 08:06 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM