comma delimited text file.

J

JustSomeGuy

Hi. I have a comma delimited text file that I want to parse.
I was going to use fscanf from the C library but as my app is written
in C++
I thought I'd use the std io stream library...

My Text file looks like:
First_Name, Last_Name, ID, Date/Of/Birth<newline>

depending on the host the newline is either \n or \r\n

so what would I do to input these lines from a file?

std::cin >> firstName >> comma >> lastName >> comma >> ID >> comma >>
dateOfBirth;

The above obviously won't work, but I'm looking for a statement similar
to this to read in the text file.

TIA.
BOB.
 
M

mlimber

JustSomeGuy said:
Hi. I have a comma delimited text file that I want to parse.
I was going to use fscanf from the C library but as my app is written
in C++
I thought I'd use the std io stream library...

My Text file looks like:
First_Name, Last_Name, ID, Date/Of/Birth<newline>

depending on the host the newline is either \n or \r\n

so what would I do to input these lines from a file?

std::cin >> firstName >> comma >> lastName >> comma >> ID >> comma >>
dateOfBirth;

The above obviously won't work, but I'm looking for a statement similar
to this to read in the text file.

You could use a std::ifstream to do the very thing you have above, or
you could use std::getline to get a whole line (into a std::string,
preferably!) and then parse it. See also FAQs 15.4 and 15.5 on error
checking with iostreams:

http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.4

Cheers! --M
 
D

Default User

JustSomeGuy said:
Hi. I have a comma delimited text file that I want to parse.

CSV files can be tricky, depending on how complicated the data is.
Specifically, whether there are embedded ',' characters that are not
delimiters.

I'd search for a C++ or possibly C implementation of a CSV library on
the web. I'm confident the problem has been solved previously. If
you're doing this an an exercise or training tool, state that and we
can reapproach it.





Brian
 
L

LR

JustSomeGuy said:
Hi. I have a comma delimited text file that I want to parse.
I was going to use fscanf from the C library but as my app is written
in C++
I thought I'd use the std io stream library...

My Text file looks like:
First_Name, Last_Name, ID, Date/Of/Birth<newline>

depending on the host the newline is either \n or \r\n

so what would I do to input these lines from a file?

std::cin >> firstName >> comma >> lastName >> comma >> ID >> comma >>
dateOfBirth;

The above obviously won't work, but I'm looking for a statement similar
to this to read in the text file.

Depends on what your file is like, embedded commas, what two commas in a
row means, etc., but consider using std::getline to read a line at a
time, and then using std::getline with the third parameter set to
something other than the default, like a comma, to get each comma
delimited field.

LR
 
D

Default User

LR wrote:

Depends on what your file is like, embedded commas, what two commas
in a row means, etc., but consider using std::getline to read a line
at a time, and then using std::getline with the third parameter set
to something other than the default, like a comma, to get each comma
delimited field.


If you're really going to do it, then you'll need something like a
state machine approach. As I mentioned elsewhere, unless this is an
assignment I recommend looking for a library.



Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top