Reading CSV Files

I'm learning C++ with the book C++, How To Program 5/e from Deitel and i like very much to develop file i/o programs, then i want to know how i can develop a program that reads and lists all the things in a CSV file, remember that i want a program that removes the " " from the file, remember too that the collums are: Name | Last Name | Password, if you want here is a example of CSV file that i want to read:

Nathan Paulino Campos
Last edited on
use getline( yourfilestream, astring, ',' ); to get the values. Then you can easily remove the quotes from astring
Hello Bazzy,
Thanks for yor useful answer, but how i can do this, you can post a big part of the code? Because i'm learning C++ and i didn't understand so much that you type.

Nathan Paulino Campos
Here is an example assuming all the values have double quotes and they don't contain commas
ifstream file ( "file.csv" ); // declare file stream:
string value;
while ( file.good() )
     getline ( file, value, ',' ); // read a string until next comma:
     cout << string( value, 1, value.length()-2 ); // display value removing the first and the last character from it

Tutorial on file I/O:
To display values in even columns you may use some manipulators ( )
If each three values you have a newline in your file, you should remove it ( )
Unfortunately, reading a CSV record from file is easily one of the trickier things to do -- iff you intend to be able to read any field datum.

If you are just working with simple CSV file, where the field separator is never part of the field itself, then you can start with something like the code I wrote here:

If you want something fancier, you'll need to be much more careful. I was working on something that does it nicely -- it just needs a few more tweaks before I'm ready to release it.

Some further notes:

The quotes surrounding your CSV fields are strictly unnecessary. CSV fields are always strings -- the use of quotes (the 'field delimiter') is only necessary when you wish to embed other special characters within the field. (But there is nothing wrong with quoting every field as you have it...)

Here's some information from the documentation for my CSV library

DSV files are deceptively simple. They are organized by four rubrics.

* A file is a listing of records which are separated by record separator character(s). Typically a record separator is the newline character(s) typical for your platform. Blank lines are ignored.

* Each record is composed of fields which are separated by the field separator character. Typically this is a comma or a horizontal tab character. Blank fields are not ignored.

* Each field may be surrouned by field padding characters. Typically this is any sequence of whitespace characters not found in the record separator or field separator. Internal field padding is preserved. Blank lines may contain field padding.

* The contents of the field may be quoted using a field delimiter character. Typically this is the double-quote (").

What this means is a field may contain any sequence of characters, including record separators, field separators, field padding, and field delimiter characters —even binary data!

To use the field delimiter in the field, it must be doubled: For example:

"He said ""Hello"""
Decoded, this field contains the sequence:

He said "Hello"

And that's it!
Well, there is one further consideration. C and C++ programmers are accustomed to using certain character sequences in textual data to represent unprintable or otherwise unusable characters. This has often resulted in DSV files that use those same character sequences.

So, we'll have to add a fifth rubric:

* It is possible that a field contain escape sequences. Escapes are not constrained by field delimiters.

The escape sequences are described in more detail below, but here are two ways a C or C++ programmer would expect to encode the previous example:

"He said \"Hello\""
(using the field delimiter)
He said \"Hello\" (without using the field delimiter)

Hope this helps.

Argh. Enough for now.
Thanks to all, Bazzy because of the code and the links to i learn more and learn about that line of code and Duoas for giving me a very nice link and explanation.

Nathan Paulino Campos
Topic archived. No new replies allowed.