Csv output to modify existing file

I've been working with several I/O operations, such as inputting data from csv files to two dimensional arrays. What I am attempting to accomplish is to read in the data from the csv, hand the values off to the 2d array, and modify the array values before replacing the values initially found within the csv file.

So far, I've managed to read in the values, feed them to the array, and output them to confirm that it works, but attempts to modify the file after the fact results in a blank csv file, or with the modifications intended and the loss of all other data. Any help would be appreciated.

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
42
43
44
45
46
47
48
49
50
51
  #include <iostream>
#include <fstream>
#include <sstream>
#include<string>
#include <iomanip>
using namespace std;
int main()
{
    int array[3][4]={};
    string line, val;
    ifstream file("T.csv");
    //read data in from csv file to array
    for(int row = 0; row < 3; ++row)
    {
        getline(file, line);
        if ( !file.good() )
            break;

        stringstream iss(line);

        for (int col = 0; col < 4; ++col)
        {
            getline(iss, val, ',');
            if (!iss)
            {
               cout<<"Breaking...\n";
               break;
            }

            stringstream convertor(val);
            convertor >> array[row][col];
        }
    }
    //check array values
    for (int y=0; y<3; y++)
    {
        for (int x=0; x<4; x++)
        {
            cout<<array[y][x];
            if (array[y][x]<10 && x!=0)
                cout<<" ";//add space for single digit values
            if (x!=3)//add commas when not at end of row
                cout<<", ";
        }
        cout<<endl;
    }
    //increment first field and hand off to csv file
    array[0][0]++;
    ofstream output("T.csv",ios_base::app);
    output<<array[0][0];
}

You can see here how I attempted to flag the output file for appending, but it only results in additions to the first field. I'm sure the answer lies with getting it to separate fields between commas, but haven't figured out how to implement it yet. Also, ios::app doesn't seem to be what I need. It may be simpler to feed the entire array back to the output file, replacing all values within it with the new array values. Any ideas on how this can be accomplished? Attempts to use stringstream convertor in reverse have failed, although it is possible I did not do it correctly.

Another issue that may be apparent upon executing this code with a file named "T.csv" in it's path, is that attempting to output to the file screws up its ability to read values into the array in the previous code. If the section attempting to output to the file is commented out (line 50), the values within the csv file will be read properly into the array. My test input file currently looks like this:
1,2,3,4
5,6,7,8
9,8,7,6

With the code above, I get all zeroes and my T.csv file only contains a 1 afterwards, but if line 50 is commented out, my output looks the same as my input file once I put the values back in the file, with the exception that spacing is added for single digit values to make them all align with one another regardless of whether 2 digit values were present (lines 40-41).
Last edited on
After a bit more experimentation, I figured it out. The file cannot be open in notepad or excel if it is to be modified. A bit of code reworking, and everything works properly.
My input file:
5,6,7,8,9,10,11,12,
13,12,11,10,9,8,7,6,
5,7,9,11,13,11,9,7,
6,8,10,12,10,8,6,7,
10,13,13,13,13,13,13,13,
13,13,13,13,13,13,4,13,
13,13,13,13,13,13,13,13,

main.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <fstream>
#include <sstream>
#include<string>
#include <iomanip>
using namespace std;

int main()
{
    float array[7][8]={};
    ifstream file("Data.csv");
    for(int row = 0; row < 7; ++row)
    {
        string line;
        getline(file, line);
        stringstream iss(line);

        for (int col = 0; col < 8; ++col)
        {
            string val;
            getline(iss, val, ',');
            if ( !iss )
                break;
            stringstream convertor(val);
            convertor >> array[row][col];
        }
    }
    //output array before altering
    cout<<"Old Data:\n";
    for (int y=0;y<7;y++)
    {
        for(int x=0;x<8;x++)
        {
           cout<<array[y][x]<<" ";
           if (array[y][x]<10)
                cout<<" ";
        }
        cout<<endl;
    }
    //increment all values and send back to file
    ofstream out("Data.csv");
    for (int y=0;y<7;y++)
    {
        for (int x=0;x<8;x++)
        {
            array[y][x]++;
            out<<array[y][x]<<",";
        }
         out<<endl;
    }
    //output new data sent to file
    cout<<"\nNew Data:\n";
    for (int y=0;y<7;y++)
    {
        for(int x=0;x<8;x++)
        {
            cout<<array[y][x]<<" ";
            if (array[y][x]<10)
                cout<<" ";
        }
        cout<<endl;
    }
    cout<<"Open file Data.csv to confirm the above data.";
}

When I run the code, and the csv file is not open in another program, my output now looks like this:
Old Data:
5  6  7  8  9  10 11 12 
13 12 11 10 9  8  7  6 
5  7  9  11 13 11 9  7
6  8  10 12 10 8  6  7
10 13 13 13 13 13 13 13
13 13 13 13 13 13 4  13 
13 13 13 13 13 13 13 13

New Data:
6  7  8  9  10 11 12 13 
14 13 12 11 10 9  8  7
6  8  10 12 14 12 10 8 
7  9  11 13 11 9  7  8
11 14 14 14 14 14 14 14
14 14 14 14 14 14 5  14
14 14 14 14 14 14 14 14
Open file Data.csv to confirm the above data.

When I open the file in notepad or excel after execution, I can confirm that the numbers have changed and the 13's are now 14's, etc. If the file is not open elsewhere, on each execution the numbers grow larger, and I can modify the values in either notepad or excel prior to execution if I wish, which is what I was trying to accomplish, and was easier once I appended each value with a comma even when finishing a row.
Last edited on
Topic archived. No new replies allowed.