Table of contents
3. Append content to the csv file
Instructions for use: csv files are separated by ",". Therefore, "," should be avoided in each content
1. Read the csv file
C++ opens the file by reading the file. That is, open the file through the ifstream class.
string fname = "test.csv";
//以读入方式打开文件
ifstream csv_data(fname, ios::in);
if (!csv_data.is_open())
{
cout << "Error: opening file fail" << endl;
exit(1);
}
else {
string line;
vector<string> words; //声明一个字符串向量
string word;
// ------------读取数据-----------------
// 读取标题行
getline(csv_data, line);
istringstream sin;
// 按行读取数据
while (getline(csv_data, line))
{
// 清空vector及字符串流,只存当前行的数据
words.clear();
sin.clear();
sin.str(line);
//将字符串流sin中的字符读到字符串数组words中,以逗号为分隔符
while (getline(sin, word, ','))
{
cout << word << endl;
words.push_back(word); //将每一格中的数据逐个push
}
}
csv_data.close();
}
2. Write to csv file
C++ opens the file for writing through file writing. That is, write through the ofstream class, and specify ios::out in the open file.
Description: By default, write iso::out, and it will be created when the file does not exist
string fname = "test.csv";
ofstream outFile(fname, ios::out);
// 写入标题行
outFile << "name" << ','
<< "income" << ','
<< "expenditure" << ','
<< "addr" << endl;
// ********写入两行数据*********
outFile << "zhangsan" << ','
<< "3000" << ','
<< "1200" << ','
<< "陕西省" << endl;
outFile << "lisi" << ','
<< to_string(2032.1) << ','
<< to_string(789.2) << ','
<< "北京市" << endl;
//数字需转为字符串进行写入,csv文件结束一行写入需要"\n"或者endl进行换行
outFile.close();
3. Append content to the csv file
It is almost the same as part 2, except that the ios::app method is selected when opening the file. Create the file if it does not exist
ofstream outFile(fname, ios::app);
// ********写入两行数据*********
outFile << "wangwu" << ','
<< "1234" << ','
<< to_string(12.32) << ','
<< "河南省" << endl;
outFile << "lisi" << ','
<< to_string(2032.1) << ','
<< to_string(789.2) << ','
<< "北京市" << endl;
//数字需转为字符串进行写入,csv文件结束一行写入需要"\n"或者endl进行换行
outFile.close();
4. Specific use
4.1 Read in csv file
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
void PrintCSVLine(vector<string> line_data)
{
//此语法仅在C++11中适用
for (string str : line_data)
{
cout << str << " ";
}
cout << endl;
}
//读入csv文件
int main() {
string fname = "test.csv";
//以读入方式打开文件
ifstream csv_data(fname, ios::in);
if (!csv_data.is_open())
{
cout << "Error: opening file fail" << endl;
exit(1);
}
else {
string line;
vector<string> words; //声明一个字符串向量
string word;
// ------------读取数据-----------------
// 读取标题行
getline(csv_data, line);
istringstream sin;
// 按行读取数据
while (getline(csv_data, line))
{
// 清空vector及字符串流,只存当前行的数据
words.clear();
sin.clear();
sin.str(line);
//将字符串流sin中的字符读到字符串数组words中,以逗号为分隔符
while (getline(sin, word, ','))
{
//cout << word << endl;
words.push_back(word); //将每一格中的数据逐个push
}
//输出此行中的内容
PrintCSVLine(words);
}
csv_data.close();
}
}
4.2 Write to csv file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string fname = "test.csv";
ofstream outFile(fname, ios::out);
if (outFile.is_open()) // 检查文件是否打开成功
{
// 写入标题行
outFile << "name" << ','
<< "income" << ','
<< "expenditure" << ','
<< "addr" << endl;
// ********写入两行数据*********
outFile << "zhangsan" << ','
<< "3000" << ','
<< "1200" << ','
<< "陕西省" << endl;
outFile << "lisi" << ','
<< to_string(2032.1) << ','
<< to_string(789.2) << ','
<< "北京市" << endl;
//数字需转为字符串进行写入,csv文件结束一行写入需要"\n"或者endl进行换行
outFile.close();
}
else
{
cout << "文件无法打开!" << endl;
}
}
4.3 Append to csv file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string fname = "test.csv";
//先判断文件是否存在
ifstream file(fname);
if (!file.is_open()) {
cout << "File does not exist!" << endl;
return 1;
}
else {
cout << "File exists!" << endl;
file.close();//必须先关闭文件后才可写入
ofstream outFile(fname, ios::app);
// ********写入两行数据*********
outFile << "wangwu" << ','
<< "1234" << ','
<< to_string(12.32) << ','
<< "河南省" << endl;
outFile << "lisi" << ','
<< to_string(2032.1) << ','
<< to_string(789.2) << ','
<< "北京市" << endl;
//数字需转为字符串进行写入,csv文件结束一行写入需要"\n"或者endl进行换行
outFile.close();
}
return 0;
}
content reference
C++ reads and writes CSV files_c++ reads csv files_Hui Hui No. 2 Blog-CSDN Blog
If you find any deficiencies/defects/bugs in this article, please contact the author to modify