C + + read CSV file and write file

Source: Internet
Author: User

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:





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.