Thread: Can I successively read lines using fscanf?

  1. #1
    Registered User
    Join Date
    Oct 2003

    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
    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.
    if(fscanf(fp_input, "%f %f %f", &m1, &m2, &m3) != 3)
       /* handle error */

  3. #3
    Registered User
    Join Date
    Oct 2003
    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, 02:56 AM
  2. bytes lost with partial read in UDP
    By mynickmynick in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-31-2009, 02:06 AM
  3. read lines
    By jujubeats in forum C++ Programming
    Replies: 18
    Last Post: 11-02-2006, 07: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, 06:41 PM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
Website Security Test