C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-12-2009, 03:42 PM   #1
Registered User
 
Join Date: Aug 2007
Location: U.K.
Posts: 147
Reading from a .csv file.

Hi,

I have wrote a program which stores values in a .csv file.

He is the how the data is stored in the .csv file:

Quote:
"Steve","21 Main Street, Nottingham","SW1 1AB","0115 9123456","These are some details. and, so.","112233","1359"
"David Baner","123 Shaw street, Nottingham","NG92HJ","020 123456","ifbiugbuige","112233","1200"
Each line forms a single object.

What I want to do is retrieve the values from the .csv file, so that I can recreate the objects again, but after reading Google, find myself a little lost in how go about this as simply as possible.


So i was thinking of something like this:

Code:
cout << "You chose to load from the CSV file" << endl;

ofstream streamname("database.csv");
if(!streamname)
{
	cout << "Cannot open file" << endl;
	return 1;
}
else
{
	cout << "Stream opened successfully" << endl;
}


getline( streamname, storelinevariable, '\n' ); 
What datatype should I use for storing the line? ie: the 'storelinevariable' parameter?

I use the following function for adding the double quotes to the datamember values when constructing an object:

Code:
	//Virtual function for adding double quotes to strings input by the user for use in the .csv file:
	virtual void addDoubleQuotes(string& m_Name,string& m_Address,string& m_PostCode,string& m_TelNo,string& m_Details)
	{
		m_Name = '\"' + m_Name + '\"';
		m_Address = '\"' + m_Address + '\"';
		m_PostCode = '\"' + m_PostCode + '\"';
		m_TelNo = '\"' + m_TelNo + '\"';
		m_Details = '\"' + m_Details + '\"';
	}
But once a line is retrieved, this is an example of how I would like to construct the object:

Code:
Derived *ptr = new Derived(mainname, mainaddress, mainpostcode, maintelno, maindetails, maindate, maintime);
vectorname.push_back( ptr );


So if anyone could point me in the right direction, I'd really appreciate it!

Many thanks!

Last edited by Swerve; 11-12-2009 at 04:29 PM.
Swerve is offline   Reply With Quote
Old 11-12-2009, 06:09 PM   #2
Registered User
 
Join Date: Jan 2005
Posts: 7,252
>> What datatype should I use for storing the line?
A string. You pretty much have to since you're using getline there, and it seems like the right solution.

You then have to parse the string. If you don't have to worry about embedded quotes, then using a stringstream would make sense. You can read in the first quote, then use getline with the stringstream to read to the next quote (use '\"' as the delimiter instead of '\n'). Then read in the comma and the beginning quote of the next value and repeat for each one.
Daved is offline   Reply With Quote
Old 11-12-2009, 09:54 PM   #3
Registered User
 
Join Date: Aug 2007
Location: U.K.
Posts: 147
Hey Daved, thanks for helping!

I've gotten into a right mess with this and seems to be making it wayyy more complicated than it needs to be.

I need to use the individual columns, and add them into corresponding variables.

Here is what I've come up with (but would be MORE than happy to scrap it and use the Standard Library).

Code:
void Tokenize(const string& str, vector<string>& tokens, const string& delimiters = " ")
{
	string::size_type lastPos = str.find_first_not_of(delimiters, 0);
	string::size_type pos     = str.find_first_of(delimiters, lastPos);

	while (string::npos != pos || string::npos != lastPos)
	{
		tokens.push_back(str.substr(lastPos, pos - lastPos));
		lastPos = str.find_first_not_of(delimiters, pos);
		pos = str.find_first_of(delimiters, lastPos);
	}
}
(I found that function on the internet)
Code:
case 'd':

	{
		//---------------
		//Load from CSV file
		//---------------
		cout << "You chose to load from the CSV file" << endl;
		ifstream file("database.csv");

		
		string completelinefromcsv;
		do
		{
		getline(file,completelinefromcsv);
		vector <string> loadvector;

		int a = 0;

		cout << completelinefromcsv << endl;

		Tokenize(completelinefromcsv, loadvector, ",");//put the separate strings into the vector

		string mainname;
		mainname = loadvector[a];
		//a++;
		//string mainaddress = loadvector[a];
		//a++;
		//string mainpostcode = loadvector[a];
		//a++;
		//string maintelno = loadvector[a];
		//a++;
		//string maindetails = loadvector[a];
		//a++;
		//string maindate = loadvector[a];
		//a++;
		//string maintime = loadvector[a];

		}
		while(getline(file,completelinefromcsv));

		file.close();

		break;
	}
When I run the code, it compiles, then reports saying "vector subscript out of range".

I tried the stringstream(), but got it all wrong, so ended up trying the above.

Like I say, I'm trying to get each column into the variables:

Quote:
mainname;
mainaddress;
mainpostcode;
maintelno;
maindetails;
maindate;
maintime;
Any help is real appreciated, especially if I can scrap all that cr..........y effort and use the standard library. Using the stringstream would be great if anyone could help me with that.

I've got to the stage where I'm looking at it and my brains like *blank*.

Last edited by Swerve; 11-12-2009 at 09:57 PM.
Swerve is offline   Reply With Quote
Old 11-12-2009, 11:48 PM   #4
Registered User
 
Join Date: Jan 2005
Posts: 7,252
Show what you did on the stringstream. I laid out the steps quickly in my last post. Try to follow those steps and post what you get. I think that's the best option for you. Tokenizing on the comma will not work so well because there are embedded commas.
Daved is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
opening empty file causes access violation trevordunstan C Programming 10 10-21-2008 11:19 PM
Formatting a text file... dagorsul C Programming 12 05-02-2008 03:53 AM
Formatting the contents of a text file dagorsul C++ Programming 2 04-29-2008 12:36 PM
Possible circular definition with singleton objects techrolla C++ Programming 3 12-26-2004 10:46 AM
System drdroid C++ Programming 3 06-28-2002 10:12 PM


All times are GMT -6. The time now is 05:39 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22