C++读取.txt并保存csv

要读取的 .txt 文件类似这种data.txt:

-83.635047	 -248.884343	 -15.649366
-83.237317	 -248.651216	 -15.458514
-82.838610	 -248.419124	 -15.266673
-82.438903	 -248.188020	 -15.075844
-82.039185	 -247.956894	 -14.886015
-81.638502	 -247.727802	 -14.695220
-81.236808	 -247.499677	 -14.506436
-80.835137	 -247.272574	 -14.316676
-80.432466	 -247.046460	 -14.127926
-80.028807	 -246.821357	 -13.939188
-79.624149	 -246.597244	 -13.751461
-79.219502	 -246.374130	 -13.563757
-78.813867	 -246.153006	 -13.377087
-78.407233	 -245.931893	 -13.190406
-77.999622	 -245.712792	 -13.003759
-77.592011	 -245.494667	 -12.818134
-77.182412	 -245.277567	 -12.632510

以上数据文本有如下特点,每行数据个数相同,数据之间采用空格隔开,读取该数据文件的方式有逐词读取、逐行读取。

#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;

int main()
{
    // 逐词读取,按照空格进行分隔
    ifstream fin("data.txt");
    string s;
    while(fin>>s) 
    {
        cout<<s<<endl;
    }
    fin.close();
    s.clear();
    
    // 逐行读取,按照每行结束的回车区分
    fin.open("data.txt");
    while(getline(fin,s)) 
    {
        cout<<s<<endl;
    }
    fin.close();
    s.clear();
    return 0;
}

在这里插入图片描述

		// 逐词读取,按照空格进行分隔
	ifstream fin("bottom.txt");
	string s;
	while (fin >> s)
	{
		cout << s << endl;
	}
	fin.close();
	s.clear();


	// 逐行读取,按照每行结束的回车区分
	fin.open("bottom.txt");
	while (getline(fin, s))
	{
		cout << s << endl;
	}
	fin.close();
	s.clear();

逐行读取操作数据不灵活,逐词读取数据体量又太大,那么自然想到先逐行,再从每一行中采用逐词读取的方式提取到感兴趣的数据。

	ifstream fin;
	istringstream iss;
	string s;
	double t;
	vector<double> tube;
	int count = 0;
	// 逐行读取,将每一行数据读取到字符串 s 中
	fin.open("bottom.txt");
	while (getline(fin, s))
	{
		iss.clear();
		iss.str(s);
		// 逐词读取,遍历每一行中的每个词 t
		while (iss >> t)
		{
			cout << t << " ";
		}
		cout << endl;
	}

在这里插入图片描述
按照上述方式,就可以提取到你感兴趣的数据,比如抽取该data.txt文件的第一二三列数据,并存放到bottom.csv文件中(方便 Matlab 绘制曲线用):

	ifstream fin;
	ofstream outFile;           // 2、创建流对象
	istringstream iss;
	string s;
	double t;
	vector<double> tube_x;
	vector<double> tube_y;
	vector<double> tube_z;
	int count = 0;
	// 按行读取,将每一行数据读取到字符串 s 中
	fin.open("bottom.txt");
	while (getline(fin, s))
	{
		iss.clear();
		iss.str(s);
		// 逐词读取,操作/提取 你感兴趣的数据
		while (!iss.eof())
		{
			iss >> t;
			count += 1;
			if (count % 3 == 1)
			{
				tube_x.push_back(t);
			}
			if (count % 3 == 2)
			{
				tube_y.push_back(t);
			}
			if (count % 3 == 0)
			{
				tube_z.push_back(t);
			}
		}
		count = 0;
	}
	int num = tube_x.size();
	cout << "num=" << num << endl;
	for (int j = 0; j < num; j++)
	{
		cout << "tube_x:" << tube_x[j] << "        " << "tube_y:" << tube_y[j] << "        " << "tube_z:" << tube_z[j] << endl;
		outFile.open("bottom.csv", ios::app);
		outFile << tube_x[j] << "," << tube_y[j] << "," << tube_z[j] << "\n";
		outFile.close();
	}

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用C++的文件操作和字符串处理来读取txt文本内容并将其写入csv表格。 首先,您需要包含必要的头文件: ```cpp #include <iostream> #include <fstream> #include <sstream> #include <string> #include <vector> ``` 然后,您可以编写一个函数来读取txt文件内容并将其写入csv表格: ```cpp void convertTxtToCsv(const std::string& txtFilePath, const std::string& csvFilePath) { std::ifstream txtFile(txtFilePath); std::ofstream csvFile(csvFilePath); if (!txtFile.is_open()) { std::cout << "Failed to open txt file!" << std::endl; return; } if (!csvFile.is_open()) { std::cout << "Failed to create csv file!" << std::endl; return; } std::string line; while (std::getline(txtFile, line)) { std::vector<std::string> fields; std::stringstream ss(line); std::string field; // 将line中的数据按照分隔符逐个读取到fields中 while (std::getline(ss, field, ',')) // 根据具体的分隔符修改此处 { fields.push_back(field); } // 将fields中的数据按照CSV格式写入csv文件中 for (const std::string& field : fields) { csvFile << "\"" << field << "\","; } csvFile << std::endl; } txtFile.close(); csvFile.close(); std::cout << "Conversion completed!" << std::endl; } ``` 在上述代码中,`txtFilePath`是要读取txt文件路径,`csvFilePath`是要生成的csv文件路径。您需要根据实际情况修改分隔符,例如上述代码使用的是逗号作为分隔符。 最后,您可以调用这个函数来进行换: ```cpp std::string txtFilePath = "path_to_txt_file.txt"; std::string csvFilePath = "path_to_csv_file.csv"; convertTxtToCsv(txtFilePath, csvFilePath); ``` 请将`path_to_txt_file.txt`替换为您要读取txt文件的实际路径,将`path_to_csv_file.csv`替换为您要生成的csv文件的实际路径。 这样,您就可以使用C++读取txt文本内容并将其写入生成的csv表格中了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值