Input files, delimiters, and strings with spaces

I can't find information of using a text file as input. The file contains data types of string with spaces, string with spaces, int, and double.

I am using a array of obects with each object housing string with spaces, string with spaces, int, and double variable. I can get the program to compile, but the output just displays the data from the default constructor for each instance of the array.

I know my problem is related the capturing a string with spaces and using a delimiter in a file. I can't seem to find any useful info that helps my problem.

How do I capture data from a text file formatted as below:

text text text,abc def,100,10.99
text text, rst abc,50,5.99
text text text text,abc def,100,10.99



I looked there. It didn't help. :-\
Well, there is a difference of opinion on how to solve your prob.

Personally, I would read the file line by line. split the values, and then convert the value types as required.

If you're wanting to carry of using the extraction operator to read the numbers, you'll need to use something like cin.getline() to read the text (it takes an optional param which defines the stop character).

http://www.cplusplus.com/reference/iostream/istream/getline/

If you take the extraction/getline route, you'll need more complicated loop logic to avoid problems if the file is poorly formatted.

Andy

P.S. Someone asked a very similar question recently, but I couldn't find it when I looked a moment ago.
The file wasn't opening, but the advice was helpful. I now have the code working the way it should.

Thanks!

This is the logic I used in my file extraction operator for the objects. Is there a better way?

1
2
3
4
5
6
7
8
9
istream& operator >> (istream& in, Book& object)
{
	getline(in,object.bookName,',');
	getline(in,object.bookAuthor,',');
	in >> object.bookPages;
	in.ignore(1,',');
	in >> object.bookPrice;
	return in;
}
Well, as I said above, I would read the file a line at at time and then parse it.

But as >> can't deal with arbitrary length string with spaces, you stuck with using getline for those fields. (Which I would avoid)

How does your function handle poorly formed files?

I just want to throw my two cents in, andywestken is giving you good advice. It may seem complicated when you read what he wrote but in fact the string library has native functions to accomplish what he is suggesting: http://www.cplusplus.com/reference/string/string/find/
From what it looks like your data is in what is called "CSV" format, that is "Comma Seperated Value". Parsing data that is presented like this is well documented on the internet.
It appears that this is quite a popular question! And paraphrases of "Do not mix formatted and unformatted input." are a common response!

I found the following thread on the daniweb.com forum (which gives a nice and clear decription)
http://www.daniweb.com/software-development/cpp/threads/365028

It the response from Narue (the Code Goddess) I'm referring to.

Andy

P.S. If you agree with me, that it's clear, maybe we should get her permission to lift the reponse to add to this sites documents?
@ andywestken: I think you'd do as much good by contributing an Article to this site. There was a small movement here during the last upgrade to have some of the regulars contribute expansions to the tutorials but that never went anywhere so I have a feeling the Admin isn't interested.
Topic archived. No new replies allowed.