471,092 Members | 1,135 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

comma delimited text file.

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.

Jul 28 '06 #1
4 4557
JustSomeGuy wrote:
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-lit....html#faq-15.4

Cheers! --M

Jul 28 '06 #2
JustSomeGuy wrote:
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
Jul 28 '06 #3
LR
JustSomeGuy wrote:
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
Jul 28 '06 #4
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
Jul 28 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bernie Yaeger | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.