By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,212 Members | 1,909 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,212 IT Pros & Developers. It's quick & easy.

comma delimited text file.

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.