C++ .csv文件处理 与 sstream应用
2016-03-16 22:42
531 查看
今天在window上用C++处理.csv文件时遇到了诸多问题,现将一些解决方法与相关链接整理一下。如果大家有更好的方法希望能够告诉小白我.
执行中可以看出
1:如果文件不存在是会打印”文件打开失败的 “意味着fin.eof()并不能表明文件是否打开 2:如果给一个全e的文件,sum会比counterE 大1,这其实是在文件读取完后再读取一次才能将fin.eof()置为1.也就是表明达到的文件的末尾.
这里关于stringstream的使用还有一些问题,今天补起来
类视图即引用关系:
**put()成员函数用于在尾部添加字符
str()成员函数对象返回一个string字符串
流操作符结合stringstream进行格式的转换**
istringstream
istringstream类用于执行C++风格的串流的输入操作。
2. ostringstream
3.stringstream
使用方法类似,这里用其进行格式转换
1. 一般文件的读取
利用<fstream>头文件 入门链接:http://blog.sina.com.cn/s/blog_7c2c21230100syud.html 进一步理解:http://blog.csdn.net/kingstar158/article/details/6859379 注意1:在这一段代码
ifstream fin("file.txt"); char ch; int counterE=0; int counterSum = 0; if (fin) { while (!fin.eof()) { ch = fin.get(); counterSum++; if (ch == 'e') counterE++; } } else { cout << "文件打开失败" << endl; }
执行中可以看出
1:如果文件不存在是会打印”文件打开失败的 “意味着fin.eof()并不能表明文件是否打开 2:如果给一个全e的文件,sum会比counterE 大1,这其实是在文件读取完后再读取一次才能将fin.eof()置为1.也就是表明达到的文件的末尾.
2.处理.csv文件
这里目前我大致的思路是用getline(),以','为分隔符,先读入string中再做具体的处理。 这里以遇到的处理数字为例,用stringstream将字符串变为相应的数字,这一部分参见 http://www.cnblogs.com/nzbbody/p/3504199.html,似乎效率会一直有人喷C++IO很慢,不过目前用起来很简单就够了.
ifstream file ("topo.csv"); string lineString; stringstream sLineString; string singleString; stringstream sSingleString; int temp; vector<int> V; while (file) { getline(file, lineString);//读入一行 if (file)//这里检查一次就是为了防止以上所说的即使读取完了还得再一次才能判断文件关闭 { sLineString << lineString; //将sLineString以,为分隔符赋值给singleString while (getline(sLineString, singleString, ',')) { sSingleString << singleString; sSingleString >> temp; sSingleString.clear();//每次用过stringstream后记得清楚 V.push_back(temp); } sLineString.clear(); } } for (auto i = V.begin(); i != V.end();++i) { cout << *i << endl; }
这里关于stringstream的使用还有一些问题,今天补起来
3.sstream初识
相关博客:http://blog.csdn.net/allen_fan_01/article/details/12284063类视图即引用关系:
**put()成员函数用于在尾部添加字符
str()成员函数对象返回一个string字符串
流操作符结合stringstream进行格式的转换**
istringstream
istringstream类用于执行C++风格的串流的输入操作。
//istringstream类用于执行C++风格的串流的输入操作。 //str()成员函数的使用可以让istringstream对象返回一个string字符串 istringstream istr("1 56.72"); int a; istr>>a; cout<<"a:"<<a<<endl; cout<<"读取a后的ostr:"<<istr.str()<<endl; double b; istr >> b; cout << "b:" << b << endl; cout << "读取b后的ostr:" << istr.str() << endl; //那么这里是不是恰好是这样符合读取的顺序才能正确读出呢 //以下为验证 istringstream istr2("1 56.72 2 2.5 3"); int x1, x2, x3; double y; istr2 >> y; istr2 >> x1; istr2 >> x2; istr2 >> x3; cout << "y:" << y << '\t' << "x1:" << x1 << '\t' << "x2:" << x2 << '\t' << "x3:" << x3 << '\t' << endl; //显然 上述例子说明其只是按顺序在读取 //输出结果 /* a:1 读取a后的ostr:1 56.72 b:56.72 读取b后的ostr:1 56.72 y:1 x1:56 x2:-858993460 x3:-858993460 */
2. ostringstream
//ostringstream //put()函数用于在尾部添加字符 ostringstream ostr; ostr.put('d'); ostr << "dawdawd"; cout << ostr.str() << endl; string gstr = ostr.str(); cout << gstr << endl; ostr << "xyz"; gstr = ostr.str(); cout << gstr << endl; ostringstream ostr2("qwe"); cout << ostr2.str() << endl; ostr2.put('s'); cout << ostr2.str() << endl; ostr2 << "dawdwafffgg"; cout << ostr2.str() << endl; /*输出结果 ddawdawd ddawdawd ddawdawdxyz qwe swe sdawdwafffgg */
3.stringstream
使用方法类似,这里用其进行格式转换
stringstream sstr("ccc"); sstr.put('a'); sstr.put('e'); string gstr2 = sstr.str(); cout << gstr2 << endl; int int_a; //int a; sstr >> int_a; cout << int_a<< endl; sstr >> int_a; cout << int_a<< endl;
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 如何组织构建多文件 C 语言程序(二)
- 关于指针的一些事情
- 如何写好 C main 函数
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua和C语言的交互详解
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解