For file reading and writing, of course, text file is best read, best written, no document structure to consider.
For Windows Excel files such as the operation is not so easy, we can search, basically are all complex ways to read and write.
Of course, this is not my original, in some csdn corner also have this article similar expression, we do not have to be serious.
The essential:
CSV format file, is a text file, can be used in C + + file flow simple read and write. However, text files of this format can be supported by default Excel, so it is Excel to open Excel files.
The code is as follows;
#include <fstream>
#include <string>
#include <iostream>
#include <streambuf>
using namespace std;
int main ()
{
//definition file output stream
ofstream ofile;
Open the file you want to output
ofile.open ("Scoresheet.csv", Ios::out | ios::trunc); This makes it easy to output a required Excel file
ofile << "name <<", "<<" Age "<<", "<<" Class "<<", "< < "class Teacher" << Endl;
Ofile << "John" << "<<" "<<", "<<" 1 "<<", "<<" JIM "<< Endl;
Ofile << "Dick" << "<<" "<<", "<<" 3 "<<", "<<" TOM "<< Endl;
Ofile.close ();
Open the file you want to output
ifstream ifile ("Scoresheet.csv");
String readstr (std::istreambuf_iterator<char> (ifile)), std::istreambuf_iterator<char> () );
cout << readstr.c_str ();
return 0;
}
The effect is as follows: