Quick File Input Question

This is a discussion on Quick File Input Question within the C Programming forums, part of the General Programming Boards category; Hello, Just a quick question; I need a little help with fscanf. How would you read the following line of ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    24

    Quick File Input Question

    Hello,

    Just a quick question; I need a little help with fscanf.

    How would you read the following line of text into a program?

    Code:
    4011900,"4-1.19",4.00,"01S, B19","01S, BA3T","A","0","0"
    4012200,"4-1.22",4.00,"01S, B22","01S, BA2T","A","0","0"
    This is an int, char, float, char, char, char, char, char. My current fscanf statement reads as follows:

    Code:
    fscanf( circuit_file, "%d,%[^,],%f,%[^,],%[^,],%[^,],%[^,],%[^,]", ...);
    I left out the last part because I do not have any problems with my variable declarations. Where I'm running into problems is reading in "01S, B19". This should be read in as one char, but is being read in as two because of the comma and/or space. Any suggestions?

    Thanks!!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If the quotation marks are meant to additionally delimit (rather than be part of) the desired string, then perhaps something like this.
    Code:
     while ( fscanf( file, "%d,\"%[^\"]\",%f,\"%[^\"]\",\"%[^\"]\",\"%[^\"]\","
                          "\"%[^\"]\",\"%[^\"]\"", &a, b, &c, d, e, f, g, h) == 8 )
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    24
    Thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. File input in C++ - S1mple question...
    By darknite135 in forum C++ Programming
    Replies: 4
    Last Post: 12-23-2007, 07:44 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 10:28 PM
  5. quick question: File Input
    By meltingdude in forum C Programming
    Replies: 1
    Last Post: 04-08-2003, 02:02 AM

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