473,396 Members | 1,892 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,396 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 4763
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.