Read from file of unknown length, then terminate

This is a discussion on Read from file of unknown length, then terminate within the C Programming forums, part of the General Programming Boards category; I cannot figure out how to use fscanf to understand when my loop has run through all the numbers in ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    16

    Read from file of unknown length, then terminate

    I cannot figure out how to use fscanf to understand when my loop has run through all the numbers in a file.

    I tried to use

    while(fscanf(ifile, "%d", &num) !=EOF)

    but it did not work properly

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    It's probably almost working... but if there are non-numeric things in the file the loop will never progress. You should check the returned value. If it is 1, it was a number. If it was a zero (but still not EOF), whoops.... some crappola in the file. Now you have to somehow skip over that.

    Here's a little intro I found doing a quickie search: http://www.cs.bu.edu/teaching/c/file-io/intro/

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    16
    thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. How to convert char read from file into string
    By cruxxe in forum C Programming
    Replies: 7
    Last Post: 05-22-2002, 02:09 PM

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