reading, modifying data within .csv file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 35
Reputation: integer*09 is an unknown quantity at this point 
Solved Threads: 0
integer*09 integer*09 is offline Offline
Light Poster

reading, modifying data within .csv file

 
0
  #1
Jan 14th, 2009
Hi all,

Good day to all.

I trying to do a program using VC++ window form that involved loading the data from .csv file and displayed it into the window form with labels. Vice versa, when the user key in data, four of the below show column values will changed, i can made the values changes accordingly within the form.

However, i do not know how to do the saving of the data back to csv or load the data from csv into window form.

Below shown is the sample containing the 1st row of 28 column then every row are the same with different values. And its fixed with 38 row by 28 column of data.

Lets make it simple say i want to change the value in column 6( which is 4) to 8 and column 1 (which is Name) to john, how can i do it simply if i were to do it with all 38 rows. The last three column are also a headache as they contained lot of commas and its not fixed.

Any advices, pointers will be greatly appreciated. Thanks in advance.

"Name",,"0","0",,"4","1","5",,"3","1","1","5",,"1","2","2","1","6",,"5",,"21",,"","1,2,16,20,21","7,9,13,17,18","3,6,11,12,14,15"
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: reading, modifying data within .csv file

 
0
  #2
Jan 15th, 2009
To change the value you have to completely rewrite the entire file. You will have to write the file back out in the same format from the data that is in the Form.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 35
Reputation: integer*09 is an unknown quantity at this point 
Solved Threads: 0
integer*09 integer*09 is offline Offline
Light Poster

Re: reading, modifying data within .csv file

 
0
  #3
Jan 15th, 2009
Originally Posted by Ancient Dragon View Post
To change the value you have to completely rewrite the entire file. You will have to write the file back out in the same format from the data that is in the Form.
Thanks for your reply.

Currently, i am doing it the entire file way thought of shortening it if possible, cause its 38 by 28 data to be rewrote. since its from you, i believed the only way is the entire way lol

How about loading it back to window form, any pointers on this?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: reading, modifying data within .csv file

 
0
  #4
Jan 15th, 2009
>>How about loading it back to window form, any pointers on this?
Not really, without knowing how you are doing it now.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 35
Reputation: integer*09 is an unknown quantity at this point 
Solved Threads: 0
integer*09 integer*09 is offline Offline
Light Poster

Re: reading, modifying data within .csv file

 
0
  #5
Jan 15th, 2009
Originally Posted by Ancient Dragon View Post
>>How about loading it back to window form, any pointers on this?
Not really, without knowing how you are doing it now.
Now i have done with the entire writing of the 1st row, to be frank, i am not really sure how to load it back.

edit: since i know which line and column the data is in within the .csv file, any idea on how to directly get to say line 6, column 8 with all those commas in the line?

Thanks in advance.
Last edited by integer*09; Jan 15th, 2009 at 4:49 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: reading, modifying data within .csv file

 
0
  #6
Jan 15th, 2009
Originally Posted by integer*09 View Post
Now i have done with the entire writing of the 1st row, to be frank, i am not really sure how to load it back.

edit: since i know which line and column the data is in within the .csv file, any idea on how to directly get to say line 6, column 8 with all those commas in the line?

Thanks in advance.
You can't go directly to row/column in a text file. The only way to do it is to sequentially read each row until you get to the one you want. One way to do this might be something like this:
#include <string>
#include <fstream>
#include <sstream>
...
...
std::string line;
ifstream in("file.csv");
int lineToGet = 6;
// read all lines until you get to
// the one you want
for(int i = 0; i < lineToGet; i++)
    getline(in,line);
// now that we have the line we want,
// split it into columns
stringstream str(line);
std::string column;
for(int i = 0; i < 5; i++)
   getline(str, column, ',');
// now, the 5th column is in variable column. 
// Since its enclosed in double quotes you will
// have to strip those out before adding to your Form
Last edited by Ancient Dragon; Jan 15th, 2009 at 5:59 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 35
Reputation: integer*09 is an unknown quantity at this point 
Solved Threads: 0
integer*09 integer*09 is offline Offline
Light Poster

Re: reading, modifying data within .csv file

 
0
  #7
Jan 15th, 2009
I have the below code almost similar to yours but the value shown in the textbox is always "True", however if i remove the .c_str from it, i will get "error C2665: 'System::Convert::ToString' : none of the 37 overloads could convert all the argument types."

Anyone knows where it goes wrong???

std::string line;
int colCnt = 0, lineCnt = 0;	
ifstream in("tsing.csv",ios::binary);

while (getline(in,line,'\n'))
{ 
lineCnt++;
if (lineCnt==4)
{
stringstream sstr(line);
std::string coln;
getline(sstr,coln,'"');
testBox1->Text = Convert::ToString(coln.c_str());
}
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: reading, modifying data within .csv file

 
0
  #8
Jan 15th, 2009
why do you need Convert::ToStrig()? coln is already a string, just send it to testBox1->Text.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 35
Reputation: integer*09 is an unknown quantity at this point 
Solved Threads: 0
integer*09 integer*09 is offline Offline
Light Poster

Re: reading, modifying data within .csv file

 
0
  #9
Jan 15th, 2009
Originally Posted by Ancient Dragon View Post
why do you need Convert::ToStrig()? coln is already a string, just send it to testBox1->Text.
I have tried to send it to textbox but i am getting the following error. Now i'm totally stuck here...

error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'std::string' to 'System::String ^'
Last edited by Ancient Dragon; Jan 15th, 2009 at 11:17 pm. Reason: disable smilies in text
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: reading, modifying data within .csv file

 
0
  #10
Jan 15th, 2009
Oh, I see you are using c++ managed code. You will have to convert char* to System::String -- I don't know how to do that because I've never written managed code. Someone else will have to help you, or you can look it up in your text book, or google for it.
Last edited by Ancient Dragon; Jan 15th, 2009 at 11:19 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC