Thread: Reading from a .csv file.

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    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:

    "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.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    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:

    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.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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