C语言 读/写.csv文件

17 篇文章 1 订阅

内部csv数据中有负数;

涉及C++中的 istringstream 的用法:


ifstream fin(fname);
int a,b,c;
string s;
getline(fin, s);
istringstream ss(s);
ss >> a>>b>>c;//把ss中的三个数赋值给a,b,c

 

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <random>
#include <sstream>
using namespace std;


int main() {

	ofstream outFile;
	outFile.open("C://Users//Desktop//er1.csv", ios::out);
	 5000*128
	for (int i = 1; i <= 10; i++) {
		for (int j = 1; j <= 10; j++) {
			outFile << -i << " " << -j << " " << i << "\n";
		}

	}
	outFile.close();
	//read();
	ifstream fin("C://Users//Desktop//er1.csv"); //打开文件流操作
	string line;
	while (getline(fin, line))   //整行读取,换行符“\n”区分,遇到文件尾标志eof终止读取	
	{
		//cout << "原始字符串:" << line << endl; //整行输出		
		istringstream iss;//istringstream提供读 string 的功能
		iss.str(line);//将 string 类型的 test 复制给 iss,返回 void 
		string s;	
		double x, y, z;
		iss >> x >> y >> z;//将读入的一行中的三个整数存储到了a, b, c中。
		cout << x << " " << y << " " << z << endl;

		/*每一行的三个数,按空格分别输出,并转化为double型数据		
		vector<double> data;
		while (iss >> s){	
			data.push_back(atof(s.c_str()));  

		}
		data.clear();*/

	}
	return 0;


}



结果:

 

 

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值