Thread: reading from a text file.

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    34

    reading from a text file.

    how do you read from a text file containing data in the following format:

    someChars,integer,SomeCharsWithOtherCommas;someCha rs,integer,.... and so on.

    I've tryed

    Code:
    fscanf(file, "%s,%d,%s;", &var1, &var2, &var3);
    but it seems to crash and i think it's from there.

    I have to read each record until the ";" at a time.

    I suspect that's where it crashes, am I right?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >someChars
    Including spaces? If so you would be better off using getline with a delimiter of ','.

    >SomeCharsWithOtherCommas
    Are they separate fields? If so, how do you tell the parser to ignore the commas since the comma is a field separator? This is an important question if you want to read the correct data.

    Does each record end with a semicolon? Maybe it would be better to read an entire record and then process it in memory at your leisure. I posted a function for reading CSV files some time ago, perhaps it may come in handy if you take the time to search for it.

    >fscanf
    Why are you using fscanf in C++ anyway?

    >&var1
    The %s flag takes a pointer to char. If var1 is an array of char then you already have that and the address-of operator is unnecessary. The same goes for var3.

    I can't be of more help without a more detailed specification of your file format. What you've given is missing some critical details that would determine the algorithm used.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    ok, there are no blanks on the first string, up to the first comma, than there is the integer value, and than there is another string containing characters, commas, blank spaces and who knows what, basically anything up to the ; goes in the last string.
    i did work before with stuff like that in c, but each record was a line and that was easy. now they're all stacked one after another, so i can't read all in a line cause i have no ideea how long it will be and besides it would create a huge string for no reason.
    i was thinking of reading each char separate and appending to strings, and than converting the middle one to integer, but this seems too tedious for a beginning c++ course and i don't think that's what they expect to see...

    sorry, forgot to add:
    yes, each record end with a semicolon...
    and
    i'm using fscanf because i don't know anything else yet.

    it's so funny, everything compiles, and than i get segmentation fault(coredump). I assume my mistake is in reading the input from the file. I'm stuck for 2 days on this now, and the stuff is due 2morrow
    Last edited by rox; 03-11-2004 at 09:46 PM.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    You know what might help? Examples. Code. "A picture is worth a thousand words" and is much more accurate an explanation.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  5. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM