C++对csv文件操作(读、写、追加)

目录

1.读csv文件

2.写入csv文件

3.向csv文件中追加内容

4.具体使用

4.1读入csv文件

4.2写入csv文件

4.3向csv文件追加

内容参考


使用说明:csv文件按照","进行分隔。因此每个内容中需避免出现","

1.读csv文件

c++通过文件读入方式打开文件。即通过ifstream类进行打开文件。


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.写入csv文件

c++通过文件写入方式打开文件进行写入。即通过ofstream类进行写入,并在打开文件中指明ios::out。

说明:默认通过iso::out方式进行写入,当文件不存在时会进行创建

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.向csv文件中追加内容

与第2部分几乎相同,只不过是打开文件时选择ios::app方式进行。当文件不存在时会进行创建

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.具体使用

4.1读入csv文件

#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写入csv文件

#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向csv文件追加

#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;
}


内容参考

C++读写CSV文件_c++读取csv文件_灰灰2号的博客-CSDN博客

若发现此文章存在不足/缺陷/BUG,请联系作者修改

猜你喜欢

转载自blog.csdn.net/qq_45929977/article/details/129831521