Help filling a structure with data from a file.

This is a discussion on Help filling a structure with data from a file. within the C++ Programming forums, part of the General Programming Boards category; Good catch, Daved, I didn't even notice that bug. I'll repost it with your fix. I guess I might as ...

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Good catch, Daved, I didn't even notice that bug. I'll repost it with your fix. I guess I might as well remove the if check too, as you pointed out, s will never be blank at that point.
    Code:
             int i = 0;
             while(getline(inDataFile, s, ','))
             {
                  p[i].price = s;
                  getline(inDataFile, s, delimiter = ',');
                  p[i].serialNum = s;
                  getline(inDataFile, s, delimiter = ',');
                  p[i].descript = s;
                  getline(inDataFile, s, delimiter = '\n');
                  istringstream stream1(s);
                  int h;
                  stream1 >> h;
                  p[i].order = h;
                  i++;
             }//!End while not end of file.

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    For more robust error checking you could read in a whole line from the file and then break it up. (Sort of like using fgets()+sscanf() instead of fscanf(). I know this is the C++ forum but that's the only example I can think of at the moment.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >For more robust error checking you could read in a whole line from the file and then break it up.
    Or you could place each of the getline's with the exception of the first one (the one in the while loop) within an if statement.

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    That's why I gave the fgets()+sscanf() vs. fscanf() metaphor. I was hoping to avoid this argument. That's what I used to say when someone said "fgets()+sscanf() is better than fscanf()!" I would say "There's no difference, in fact fscanf() is better because you don't have an intermediate buffer!" But Quzah + Dave_Sinkula (I think) convinced me otherwise. It's much easier to recover from an error if you have the whole line read in. I can't remember how they convinced me at the moment, but you can do a search on it.

    Then again, maybe that's specific to fscanf() and has no application here.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #20
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >"fgets()+sscanf() is better than fscanf()!"
    It's certainly better if you're reading from stdin. Why it would be better when reading from a file, I'm not sure. Perhaps the answer would depend on the format of your file.

  6. #21
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Why would it be better? fgets() reads from a file stream, and sscanf() parses the resulting buffer; or fscanf() reads from the file stream directly. stdin doesn't enter into the equation (unless you're thinking of scanf()).

    Yes, it does depend somewhat on the format of your file. For example, if you don't want to skip blank lines, then you have to read the line in with fgets(), because fscanf() will skip whitespace.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #22
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >stdin doesn't enter into the equation (unless you're thinking of scanf()).
    You can use fgets to read from stdin. In fact it's the only standard C library function you can use to read one line at a time without running the risk of invoking undefined behavior.

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    True, but I don't see why fgets()+sscanf() would have an advantage over fscanf() when reading from stdin as opposed to an ordinary input file stream. Unless that wasn't your point.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #24
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by dwks
    True, but I don't see why fgets()+sscanf() would have an advantage over fscanf() when reading from stdin as opposed to an ordinary input file stream. Unless that wasn't your point.
    It seems like it would be an advantage if the user is asked to enter more than one item per line. With fgets+sscanf you can ask the user to reenter the data. With fscanf you'd probably lose track of where you are.

  10. #25
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    getline() stringstream for me everytime!

  11. #26
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Quote Originally Posted by swoopy
    It seems like it would be an advantage if the user is asked to enter more than one item per line. With fgets+sscanf you can ask the user to reenter the data. With fscanf you'd probably lose track of where you are.
    Yes, that's true.

    Thank you, twomers, for unhijacking this thread.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #27
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Hey twomers, do you need boost to compile your code? I tried compiling it with Dev-C++ with no luck. I included about five headers, but still no luck.

  13. #28
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Nope, I don't have boost either ... these are the headers I had when I compiled it, I think.

    Code:
    #include <algorithm>
    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <sstream>
    
    #include <windows.h>
    Congrads on post num 2000, swoopy

  14. #29
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I don't have boost ... these are the headers I had when I compiled it, I think.
    Ok, I'll give that a shot.

    >Congrads on post num 2000, swoopy
    Woohoo, thanks twomers, and thanks to dwks, now I can relax.

  15. #30
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    It was this code, wasn't it - Help filling a structure with data from a file.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  2. Replies: 3
    Last Post: 02-26-2008, 01:12 PM
  3. ordered linked list
    By redmondtab in forum C Programming
    Replies: 48
    Last Post: 10-22-2006, 06:09 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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