Click here to Skip to main content
16,020,565 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
Hello,

I make the csv file with c#. (aaa.csv)

aaa.csv
a
b
c

And I want to add as below.

aaa.csv

a , aa
b , bb
c , cc

That is, I want to add data as a row.

Could you please tell me how to do this?
Many thanks.

What I have tried:

I tried to add data by row.

But it is written as follows.

aaa.csv

a
b
c
aa
bb
cc
Posted
Comments
CHill60 28-Aug-24 3:04am    
The "What I have tried:" section is for you to post the code that you have tried. We are more than willing to help, but you have to demonstrate that you have at least tried to do this yourself.
gggustafson 28-Aug-24 15:52pm    
Are you seeking a C# solution?
PIEBALDconsult 28-Aug-24 21:18pm    
Read it into a DataTable, update it, then rewrite the file.
snorkie 29-Aug-24 15:06pm    
Where is the code you wrote? Please include that.

aaa.csv
CSV
a
b
c
aaa.csv
CSV
a , aa
b , bb
c , cc
That isn't adding data as rows, that adding data to existing rows.

CSV is not designed for editing: it's purely a text based data transfer format, not a database. And as such, the "rows" are just a lines of text - and text files don't have a mechanism for inserting, removing, or changing text.

So you can't add data to an existing row without copying the data up to each insertion point to a new file, inserting the new data, then continuing to copy, either to the next insertion point or the end of the file, whichever comes first. You then close both files, delete or rename the original, and rename the new file to take the original's place.

If you really want to modify data frequently, not use any text based storage (CSV, XML, and JSON are all examples of text based storage) - use a "proper" database instead as they are designed for such manipulations.
 
Share this answer
 
Comments
gggustafson 28-Aug-24 15:50pm    
I'm not sure that I agree with you regarding your statement "text files don't have a mechanism for inserting, removing, or changing text". Even CSV files can be edited with a text editor, albeit with great caution.
OriginalGriff 29-Aug-24 1:21am    
Yes you can use a lot of applications to modify text files - but that doesn't mean that a text file has any built in mechanism for that. The app either does what I outlined above, or reads the whole file into memory, changes it, and writes the whole file back out.
A database file for example is designed so that changes can be made to specific parts of the file without the need to rewrite the whole thing.
gggustafson 29-Aug-24 11:44am    
There remains the problem that CSV files are a form of database. Not as advanced as say SQL but a database all the same. They are also widely used in commerce. IMHO, you needed to supply an answer akin to that which PIEBALDconsult supplied.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900