Not running well in writing data in a csv file in VS2013

I am trying to write data to a cvs file and is working great. There is only a problem that I have to close after inserting data for the next one. I was working properly on version VS2010 but now in 2013 version is not working.I mean I am inserting the first data and will be written in csv file, but when I am inserting for the second one it does not write to csv file and then I have to stop running and start from begin then will write in the file.

I do not know what is the exact problem. May you please help?

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
        String^ Number_temp = Convert::ToString(num);
        String^ personname_temp = Personnname->Text;
        String^ Start_temp = Start->Text;
        String^ End_temp = End->Text;

//Convert to char
        marshal_context ^ context = gcnew marshal_context();
        const char*number_char = context->marshal_as<const char*>(Number_temp);
        const char*personname_char = context->marshal_as<const char*>(personname_temp);
        const char*Start_char = context->marshal_as<const char*>(Start_temp);
        const char*End_char = context->marshal_as<const char*>(End_temp);

//insert to CSV
        bool flag_csv;
        StreamReader^ sr = File::OpenText("test.csv");
        String^ buff = "";
        array<Char>^chars = { ';' };
        buff = sr->ReadLine();
        if (buff == ""){ flag_csv = true; }
        else{ flag_csv = false; }
        sr->Close();
        if (flag_csv == true)
        {
            ofstream myfile;
            myfile.open("test.csv");
            myfile
                /*0*/ << personname_char << ";"
                /*1*/ << Start_char << ";"
                /*2*/ << End_char << "\n";
            myfile.close();
        }
        else if (flag_csv == false)
        {
            ofstream myfile;
            myfile.open("test.csv", ios::app);
            myfile
                /*0*/ << personname_char << ";"
                /*1*/ << Start_char << ";"
                /*2*/ << End_char << "\n";
            myfile.close();
        }
Last edited on
Topic archived. No new replies allowed.