Thread: CSV Parsing

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    93

    CSV Parsing

    Hi,

    I am trying to get information from a CSV file, and import it into my program. The code I have so far is listed below:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main () {
      string line; //this will contain the data read from the file
      ifstream myfile ("example.csv"); //opening the file.
      if (myfile.is_open()) //if the file is open
      {
        while (! myfile.eof() ) //while the end of file is NOT reached
        {
          getline (myfile,line); //get one line from the file
          //DO THE CSV PARSING HERE.
         //FOR NOW I WILL JUST OUTPUT THE LINE:
          cout << line << endl; //and output it
        }
        myfile.close(); //closing the file
      }
      else cout << "Unable to open file"; //if the file is not open output <--
    
      return 0;
    }
    How does this look? Im very new to coding, so I was wondering if anyone might be able to give some input on how I can do the CSV parsing. Are there tutorials online for this? Would anyone be able to give some insight?

    Regards,

    James

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Using .eof to control a loop is a Bad Idea, inasmuch as it doesn't actually work.

    Given a generic CSV file, all you do is look for commas (and new lines, of course). If you're allowed something like 1234,"Lastname, Firstname", 1 with a comma inside quotes that doesn't count then you'll have to be a little more careful.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    It is C++? I had some help on another forum, im really new to this but I assumed it was ok.

    Is it easy to convert this to C?

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    if this isn't part of some larger project that actually is in C++ i would suggest this:

    http://us3.php.net/manual/en/function.fgetcsv.php

    using C++ to do a job like this is like calling in a nuclear strike to smoosh a bug.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Hi,

    Thank you for the reply. I have to use C unfortunatly, but I really dont want to branch into C# or C++ as I want to take it one step at a time.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Grr... you have to use C? Right, you made me move this for nothing... but why did you start by writing it in C++ if you have to use C?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Sorry. Ive been getting help on this project to try and help me learn how to start making a simple program. This is what I have so far except I didnt realise it had branched off from C. I would like this to be in the C forum please as that is what I want it to be coded in.
    Last edited by spadez; 02-03-2009 at 12:06 PM.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by tabstop View Post
    Using .eof to control a loop is a Bad Idea, inasmuch as it doesn't actually work.
    Please elaborate on that. What's wrong with
    Code:
    filename.getline( buffer, n );
    while( !filename.eof() ) {
        ...[do stuff]...
        filename.getline( buffer, n );
    }
    ?

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by R.Stiltskin View Post
    Please elaborate on that. What's wrong with
    Code:
    filename.getline( buffer, n );
    while( !filename.eof() ) {
        ...[do stuff]...
        filename.getline( buffer, n );
    }
    ?
    I have two objections to that:

    1. Code duplication of the getline() call leads to brittleness.
    2. The use of eof() is simply unnecessary, period.

    Code:
    while(filename.getline(buffer, n))
    {
        ...[do stuff]...
    }
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by brewbuck View Post
    I have two objections to that:

    1. Code duplication of the getline() call leads to brittleness.
    2. The use of eof() is simply unnecessary, period.

    Code:
    while(filename.getline(buffer, n))
    {
        ...[do stuff]...
    }
    I don't doubt you, and I've definitely used that construct in the past, having simply copied it from texts without really understanding what it was doing. Now that I'm actually thinking about what these functions return, I can't find anything that says that [ifstream].getline() or .read() ever return anything other than the stream *this. Presumably they return 0 or NULL when they fail to extract any characters?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by R.Stiltskin
    Presumably they return 0 or NULL when they fail to extract any characters?
    No, they still return the stream by reference. However, the stream is convertible to void*, and through that conversion it is convertible to bool, and thus would evaluate to false when the eof bit (and other status bits) is set.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    So apparently, first there is
    (1) an implicit conversion from member reference to void*, and then
    (2) a second conversion from void* to bool?

    For some reason I find this interesting enough to have spent over an hour googling around for further clarification. So far, I haven't found anything to explain conversion (1), and for conversion (2) the best that I've come up with is from the C++ draft standard:
    4.12 Boolean conversions
    An rvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to an rvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. An rvalue of type std::nullptr_t can be converted to an rvalue of type bool; the resulting value is false.
    Taking 4.12 at face value, in order for conversion (2) to result in false, that has to be a conversion from a null pointer value...

    ... so conversion (1) must have resulted in a pointer with a null value ...

    ... so therefore should I conclude that a reference to a class member can be implicitly converted to a void* with a NULL value if least one of the fail, bad, or eof flags is set, and a non-NULL value otherwise?

    (Why is it so difficult to find this written somewhere?)

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by R.Stiltskin
    So far, I haven't found anything to explain conversion (1)
    Check section 27.4.4, in particular subsection 27.4.4.3, of the 2003 edition of the C++ Standard:
    operator void*() const;
    Returns: If fail() then a null pointer; otherwise some non-null pointer to indicate success.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    You're my favorite witch! Thanks, laserlight.

    Still, it irks me that an everyday usage that "everyone" is supposed to know "if(bla.read(...)" should require so much effort to find out why it works -- as opposed to "if(!bla.eof())" which is well-documented and easy to understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CSV parsing - Can you look at my code?
    By spadez in forum C Programming
    Replies: 2
    Last Post: 02-05-2009, 06:57 AM
  2. read in csv file
    By gums in forum C Programming
    Replies: 5
    Last Post: 05-10-2007, 07:38 AM
  3. draw tree graph of yacc parsing
    By talz13 in forum C Programming
    Replies: 2
    Last Post: 07-23-2006, 01:33 AM
  4. Parsing for Dummies
    By MisterWonderful in forum C++ Programming
    Replies: 4
    Last Post: 03-08-2004, 05:31 PM
  5. I hate string parsing with a passion
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-19-2002, 07:30 PM