![]() |
| | #1 | |
| Registered User Join Date: Aug 2007 Location: U.K.
Posts: 147
| Reading from a .csv file. 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:
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' ); 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 + '\"';
}
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 | |
| | #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 | |
| | #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);
}
}
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;
}
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:
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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |