About output .csv file

Ex:
I open a .csv file with contents:
1 | John | 123
2 | Peter | 142
and I want to add a field after them, like:
1 | John | 123
2 | Peter | 142
3 | Edward | 154

how can I do?
oh, thanks so much.
Now, I have a problem. Ex: the file with contents:

ID|Name |Info

1 | John | 123
2 | Peter | 142
3 | Edward | 154

If I input an existed ID, the program will remind me "ID already exists". I don't know how to check it.
Sorry, I'm a newbie.
Last edited on
help me, please!
provide the code that produces the problem
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
	{
		char ID[256],name[256];
		
		int ID_length=0;
	
		cout<<"ID: ";
		cin>>ID;
		ID_length=strlen(ID);
		
		char *check=new char[ID_length];

		fstream test("test.csv",ios::in|ios::out|ios::app);
			while (!test.eof())
			{
				test.get(check,ID_length);
				if (ID==check)
				{
					cout<<"ID already exits";
					break;
				}
			}
		
			test.close();
		system("pause");
		
}

It doesn't work.
Last edited on
First: you cannot compare c strings like on line 23. Use strcmp
Second: ID_length is too small. It leaves out the necessary terminating 0 hence it is ID_length+1 on line 17 and 22

Oh, thanks so much :D
but ex:
ID |NAME|INFO

1|peter | 124

If I input ID=2, it display "ID...." because in INFO column, "124" has "2" although the file has only one person. Can u help me with it? I have no idea.
Last edited on
You must be specific about what you need.

If you wish to manage the data table, then you should

(1) Read the entire file into memory
(2) Modify the stuff in memory
(3) Write it back to file (overwriting the file)

Here is some reading on handling simple CSV files
http://www.cplusplus.com/forum/general/17771/#msg89650

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
typedef string            field_t;
typedef vector <field_t>  record_t;
typedef vector <record_t> table_t;

table_t read_csv( const string& filename )
{
  table_t   result;
  ifstream f( filename.c_str() );
  string   s;
  while (getline( f, s ))
  {
    record_t      rec;
    istringstream ss( s );
    while (getline( f, s, '|' ))
    {
      rec.push_back( trim( s ) );
    }
    result.push_back( rec );
  }
  return result;
}

If you are going to have spaces around the fields (like your examples), then you'll need to see http://www.cplusplus.com/faq/sequences/strings/trim/ for trim functions.

Writing the CSV is simple enough:

1
2
3
4
5
6
7
8
9
10
11
12
13
void write_csv( const string& filename, const table_t& table )
{
  ofstream f( filename.c_str() );
  for (unsigned ri = 0; ri < table.size(); ri++)
  {
    f << table[ 0 ][ 0 ];
    for (unsigned fi = 1; fi < table[ ri ].size(); fi++)
    {
      f << " | " << table[ ri ][ fi ];
    }
    f << "\n";
  }
}

What's left is (2) -- if "Edward" is not in the table, then push_back() new record. Otherwise complain to the user.

Hope this helps.
Thank you, actually I'm doing a library manager program. That part is making a new account. Users input the ID, if the ID has already existed, the program would gave errors and exit, if not, ID would be written to a .csv file
Topic archived. No new replies allowed.