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"

Recommended Answers

All 10 Replies

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.

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?

>>How about loading it back to window form, any pointers on this?
Not really, without knowing how you are doing it now.

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

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 [b]column[/b]. 
// Since its enclosed in double quotes you will
// have to strip those out before adding to your Form

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());
}
}

why do you need Convert::ToStrig()? coln is already a string, just send it to testBox1->Text.

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 ^'

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.

Thanks for your reply.

lol, i dont even know that i'm using managed code. I think i will not waste time doing the conversion but i will do it in the conventional way, that is saving each data to a temp file and using the ReadAllText to show it back in the label.

If you have better methods or pointers, please let me know.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.