•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,603 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,645 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 2342 | Replies: 2
![]() |
•
•
Join Date: Sep 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
Hello! Please help me!!!
I have been trying to make a program that show details written on a csv file. Somehow I have managed to do so, by using a code for opening *txt files. However, I would also need to add details to the same csv file, and delete details as well. I've already learned how to sort alphabetical strings by the help of this forum, so thanks in advance!
Let's say menu says
1 Open csv file
2 add data on csv file
3 delete data on csv file
when you click on two it will ask you to enter a name. it then gets added to the csv file. i dunno, i can't explain it really clearly, but hope you can help!
thanks!
I have been trying to make a program that show details written on a csv file. Somehow I have managed to do so, by using a code for opening *txt files. However, I would also need to add details to the same csv file, and delete details as well. I've already learned how to sort alphabetical strings by the help of this forum, so thanks in advance!
Let's say menu says
1 Open csv file
2 add data on csv file
3 delete data on csv file
when you click on two it will ask you to enter a name. it then gets added to the csv file. i dunno, i can't explain it really clearly, but hope you can help!
thanks!
Don't think of a csv file as anything other than a file. You can access it and everything just like any other file (text file you mentioned).
What you want to do is not hard but it requires more than appending data. The simplest way of managing this (I reckon), is to create a vector of a vector of strings:
std::vector< std::vector<std::string> > csv_file_contents;
don't forget to #include <vector> and <string>. First thing is to read in the current file. I reckon line by line and tokenise with comas. The manipulation becomes much easier then. You can .erase() or .insert() or push back easily enough. Then save the contents of the csv file each time.
Note - this is not a good solution if you have a large csv file! And a better method would be to use two files - one is your first file, and the second is a progress-file. Say you want to change a line. Read each line in your first file and save to the second until you come to the line you want to change (which could be inserting or deleting lines/columns, cells etc). Then you can change that line and save to the second file. Read the rest of the first file until it's gone. Then close the streams. Delete the first file. Rename the second and you have it. You could also use fstream as opposed to if and ofstreams.
What you want to do is not hard but it requires more than appending data. The simplest way of managing this (I reckon), is to create a vector of a vector of strings:
std::vector< std::vector<std::string> > csv_file_contents;
don't forget to #include <vector> and <string>. First thing is to read in the current file. I reckon line by line and tokenise with comas. The manipulation becomes much easier then. You can .erase() or .insert() or push back easily enough. Then save the contents of the csv file each time.
Note - this is not a good solution if you have a large csv file! And a better method would be to use two files - one is your first file, and the second is a progress-file. Say you want to change a line. Read each line in your first file and save to the second until you come to the line you want to change (which could be inserting or deleting lines/columns, cells etc). Then you can change that line and save to the second file. Read the rest of the first file until it's gone. Then close the streams. Delete the first file. Rename the second and you have it. You could also use fstream as opposed to if and ofstreams.
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
I am the Walrus!
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- dat,txt file and csv file???? (C++)
- insert csv file into mysql through php (PHP)
- Need Help Reading a csv file created from MSExcel (C)
- Reading in a *.csv file and loading the data into an Array (Java)
- to delete contents of a text file (Java)
- parsing csv file (PHP)
Other Threads in the C++ Forum
- Previous Thread: using namespace std problem
- Next Thread: Class Program: Help with Calculating Account Usage



Linear Mode