Using fscanf to read a file one line at a time
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Using fscanf to read a file one line at a time

  1. #1
    Join Date
    Jun 2002
    Posts
    936

    Using fscanf to read a file one line at a time

    This is straight C, not C++ or MFC. I want to know and how I can use fscanf and fputs to read a data file without determining the length. Here is what I am talking about.

    My data file is in the form of

    8.5
    3.14
    9.5
    0.2
    1.1

    Now, it will be easier to do if I allow user to enter the length of the data, but I don't want that. I simply want user to enter the file name.

    I would like to put fscanf inside a while loop and increment a counter for each line. After the end of the last line, I want to use malloc to allocate space for an array and put the value to that array. I want to know if it is possible and how I can do it.

  2. #2
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    Sure you can do it.

    But you should probably not use any scanf() function in any serious application!!!

    If you know how to read in a float or double value from the keyboard, you should easily findout how to do it from a file.

    Hint: %f or %lf

    NOTE: scanf format codes work a bit different than printf's on floating-point numbers
    Last edited by j0nas; August 21st, 2003 at 05:31 PM.

  3. #3
    Join Date
    Jun 2002
    Posts
    936
    I was think creating a dummy buffer to initialize like

    float *x;

    then allocate memory of 2 for it, then every time I read a new line, I realocate by the counter.

    The problem is I have to read each and save it to a buffer or array.

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    [Moved thread]
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  5. #5
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    Yepp, you can reallocate space each iteration (but it may not be the best method)...

    Use realloc()... something like this:
    Code:
    float *x = NULL;
    int i = 0;
    
    do {
        x = realloc(x, i + 1);
    } while (fscanf(stdin, "%f", &x[i++]) == 1);
    printf("Got %d rows of floats.\n", i);

  6. #6
    Join Date
    Jun 2002
    Posts
    936
    Do you think of a better method rather than reallocate for each iterator?

  7. #7
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    You have many options, like:
    1. Precount rows in file, rewind the file and then allocate exact number of rows
    2. Using a linked list
    3. Allocate a fix number of array entries (with malloc) and let it grow if needed

    I would probably go for number 3 in this case...

  8. #8
    Join Date
    Jun 2002
    Posts
    936
    I understand number 3 and I probably go with it, but I like the idea of precount the number of row in the file. Do you know how can I do that inside a while loop? Is there any way you can show me how to do it with a snap code?

    Thank you

  9. #9
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    Method 1 requires a file (impossible on stdin)... so you basically do: (untested code)

    Code:
    for (rows = 0; fscanf(in, "%f", &f) == 1; rows++)
      ;
    rewind(in);
    x = malloc(rows * sizeof(float));
    for (i = 0; i < rows; i++)
        fscanf(in, "%f", x + i);
    Going through the file twice may or may not be a performance hit. If the file is still in the cache, it may not....

  10. #10
    Join Date
    Jun 2002
    Posts
    936
    Thank you, this is the first time I see and heard the rewind function, I have never use it. By looking at its description from MSDN, it makes a lot of sense.

    Base on what you said, I don't need while loop. The for loop should do it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center