has been feeling on the file read and write and flow of the use of unfamiliar, recently wrote a project just need to read the CSV file data and create a new CSV file, so read some of the online CSV file reading and writing operations of the article, basic understanding of the approximate operation.
reading a. csv file
Note: the. csv file is used ', ' as the delimiter, so each read to ', ' cutoff.
The code is as follows:
#include <iostream>
#include <fstream>
using namespace Std;
int main () {
string value;
Char filename[256];
cout<< "Please enter file name" <<endl;
cin>>filename;
Ifstream infile (filename);
while (Infile.good ()) {
//.csv file with "," as separator
getline (infile,value, ', ');
cout<<value<<endl;
}
return 0;
}
The results of the code run are as follows:
create a. csv file
#include <iostream>
#include <fstream>
using namespace std;
int main () {
String value = ' Create a new file! ';
Ofstream outfile;
Outfile.open ("F:\\newfile.csv", ios::out);
outfile<<value;
Outfile.close ();
return 0;
}
The results of the code run are as follows:
Open Newfile.csv Display: