473,387 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 4761
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bernie Yaeger | last post by:
Is there a way to convert a .xml file to a comma delimited text file using vb .net? Thanks for any help. Bernie Yaeger
2
by: Kenneth Koski | last post by:
Hello All, I have a comma delimited text file, which I would like to move into a SQL 2000 table . I created a DTS package in SQL Server and saved it as a VB.bas . I am writting the code in C#...
4
by: John Tyce | last post by:
Does any one know how to write the data from a dataset table to a text file? I have found a clumsy way to do it using WriteXml, XmlReader, XmlNodeReader, etc, but this seems awful clumsy. Is there a...
0
by: JeffM | last post by:
If you've seen this before, sorry for the repost, the last post had no replies- and I'm still stuck. Can anyone suggest a way to use a comma delimited text file to supply arguements to a method...
3
by: Avi | last post by:
I need to create a text file that has the data from the 10 tables in the database. The number of fields in the tables exceeds 255 and so I cannot make a new table with all the fields and then...
5
by: Mandrah | last post by:
I have a single text file with several lines of information with three items separated by commas. An example of a few lines: name1, email1, password1 name2, email2, password2 name3, email3,...
1
by: Quentin | last post by:
I want to take a comma delimited text file and export it to excel format, using the comma as the delimiter. Any help on this would be great, thank you.
4
by: Uncle Scotty | last post by:
When I use the File -Export function in Access, all it seems to do is duplicate the file. It doesn't seem to offer conversion to delimited text files. Can someone tell me how to do that kind of...
1
by: silpa | last post by:
I have a comma delimited text file which has data like this. 1, 11, "abc" ,False 2, 12, "def" ,False 3, 13, "ghi" ,False 4, 14, "jkl" ,False The name of this file is somedata.txt. Only one...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.