Thread: CSV Parsing

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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 );
    }
    ?

  3. #3
    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);
    //}

  4. #4
    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?

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