cpp创建csv格式文件完整代码
时间: 2023-05-23 09:04:21 浏览: 109
CSV文件读写 C++语言
以下是CPP语言创建CSV文件的完整代码范例:
```cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ofstream outputFile;
outputFile.open("example.csv");
// write the column headers
outputFile << "Name" << "," << "Age" << "," << "Score" << endl;
// write the data
outputFile << "John Smith" << "," << "25" << "," << "90.5" << endl;
outputFile << "Jane Doe" << "," << "30" << "," << "85.0" << endl;
outputFile << "Tom Lee" << "," << "20" << "," << "92.3" << endl;
// close the file
outputFile.close();
cout << "CSV file created successfully." << endl;
return 0;
}
```
这个程序使用了文件流(fstream)对象创建了一个example.csv文件,并在其中写入了一些测试数据。在这个例子中,我们首先输出了CSV文件的列标题,然后按行写入了数据。注意,每个字段都用逗号分隔。当我们完成后,我们关闭文件流并输出一条消息表示程序成功地创建了CSV文件。
阅读全文