Can I successively read lines using fscanf?

This is a discussion on Can I successively read lines using fscanf? within the C Programming forums, part of the General Programming Boards category; Hello all, I am a newbie to C (and anything resembling complex programming), so please excuse my dumbness. Is it ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2

    Can I successively read lines using fscanf?

    Hello all,
    I am a newbie to C (and anything resembling complex programming), so please excuse my dumbness. Is it possible to read a line from a file using fscanf, and then the next time I call fscanf (without closing the file), read the second line? Ie, if my input file is something like
    200 300 50
    10 12 1

    I have been doing fscanf(fp_input,"%f %f %f", Re1, Re2, Re3);
    then doing some processing and checking of the data, and then;
    fscanf(fp_input, "%f %f %f", m1, m2, m3);

    But seem to be getting some stange results. Any help greatly appreciated...

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    The fscanf function only accepts pointer arguments so you will need to pass the address of a floating point variable.
    You can also check the return value of fscanf.
    Code:
    if(fscanf(fp_input, "%f %f %f", &m1, &m2, &m3) != 3)
    {
       /* handle error */
    }

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    2
    Monster,
    Thanks, I knew it should be something stupid. After staring at it for ages, the most simple solutions disappear on you. . .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf: read word from file
    By Dedalus in forum C Programming
    Replies: 1
    Last Post: 06-24-2009, 03:56 AM
  2. bytes lost with partial read in UDP
    By mynickmynick in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-31-2009, 03:06 AM
  3. read lines
    By jujubeats in forum C++ Programming
    Replies: 18
    Last Post: 11-02-2006, 08:07 PM
  4. read a text file with lines of variable length
    By raymond in forum C Programming
    Replies: 7
    Last Post: 06-24-2006, 07:41 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM

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