Like Tree1Likes
  • 1 Post By Salem

Reading In File With fscanf Question

This is a discussion on Reading In File With fscanf Question within the C Programming forums, part of the General Programming Boards category; For a project, I have to read in input from a file, but the tricky part is that each line ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    16

    Reading In File With fscanf Question

    For a project, I have to read in input from a file, but the tricky part is that each line could have one of four formats:
    "char long char string int float"
    "char long string"
    "char long string int"
    "char long string int float"
    Would this fscanf statement work for reading in the format?
    Code:
    fscanf(input,"%c %ld %s %d %f%[^\n]",&todo,&id,symbol,&quantity,&price);
    Also, I want to read in line by line so that I can process the variables before moving onto the next line. Is there an easy loop I can use with fscanf to do this?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    > Would this fscanf statement work for reading in the format?
    I suggest you read the line using fgets(), then you can have 4 separate goes at parsing it with sscanf, without having to worry about back-tracking with fscanf, in case you get it wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    16
    By four separate goes, do you mean call sscanf four different times with my different formats? Then only one will work correctly depending on the format of that line correct?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    Exactly!
    Otto45 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    16
    Sweet, thank you so much, this is much easier than I was making it out to be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error on fscanf when reading a struct from a file
    By faustian_1 in forum C Programming
    Replies: 3
    Last Post: 10-23-2011, 01:18 PM
  2. strtok & fscanf: reading from file
    By Ralph Cacho in forum C Programming
    Replies: 8
    Last Post: 06-04-2011, 05:02 AM
  3. Reading file using fscanf into array
    By Sn0wcra5h in forum C Programming
    Replies: 3
    Last Post: 03-11-2010, 10:16 AM
  4. reading int and string from file fscanf
    By awesmesk8er in forum C Programming
    Replies: 5
    Last Post: 02-12-2009, 04:01 PM
  5. Ini File Reading using fscanf()
    By Mr.Modem in forum C Programming
    Replies: 5
    Last Post: 08-13-2005, 01:58 PM

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