C++读取txt文件.docx
- 文档编号:1677032
- 上传时间:2022-10-23
- 格式:DOCX
- 页数:10
- 大小:34.11KB
C++读取txt文件.docx
《C++读取txt文件.docx》由会员分享,可在线阅读,更多相关《C++读取txt文件.docx(10页珍藏版)》请在冰豆网上搜索。
C++读取txt文件
c++学习笔记—c++对txt文件的读取与写入
标签:
c++IO
2021-01-1317:
43 1880人阅读 评论(0) 收藏 举报
分类:
c/c++编程〔8〕
版权声明:
本文为博主原创文章,未经博主允许不得转载。
一、文件的输入输出
头文件fstream定义了三个类型支持文件IO:
ifstream从给定文件读取数据、ofstream向一个给定文件写入数据、fstream读写给定数据。
这些类型与cin和cout的操作一样,我们可以用IO操作符来读写文件,还可以用getline从一个ifstream读取数据。
1、getline()函数
getline的函数原型为:
[cpp] viewplain copy
1.istream& getline(istream& is, string& str, char delim);
2.istream& getline(istream&& is, string& str, char delim);
3.istream& getline(istream& is, string& str);
4.istream& getline(istream&& is, string& str);
通常我们使用getline函数读取一整行,该函数承受一个输入流和一个string对象,函数从给定的输入流中读取内容,直到遇到换行符为止,然后将所读的内容存入到个string对象中。
另外,当函数为istream&getline(istream&is,string&str,chardelim);形式时,函数遇到delim也会停顿。
2、使用文件流对象
当我们想要读入一个文件时,可以定义一个文件流对象,并将对象与文件相关联起来,每一个文件流类都定义了一个名为open的成员函数,完成一系列系统相关的操作。
open函数的原型为:
[cpp] viewplain copy
1.void open (const char* filename, ios_base:
:
openmode mode = ios_base:
:
out);
2.void open (const string& filename, ios_base:
:
openmode mode = ios_base:
:
out);
文件形式〔mode〕有一下几种:
[cpp] viewplain copy
1.ofstream outfile("E:
\\out.txt", ofstream:
:
app);
上述代码翻开out.txt文件,假如不存在,系统会创立此txt文件,并且定位到文件末尾。
翻开的文件使用完成后一定要关闭,fstream提供了成员函数close()来完成此操作。
[cpp] viewplain copy
1.#include "stdafx.h"
2.#include
3.#include
4.#include
5.#include
6.using namespace std;
7.int _tmain(int argc, _TCHAR* argv[])
8.{
9. ifstream myfile("E:
\\hello.txt");
10. ofstream outfile("E:
\\out.txt", ofstream:
:
app);
11. string temp;
12. if (!
myfile.is_open())
13. {
14. cout << "未成功翻开文件" << endl;
15. }
16. while(getline(myfile,temp))
17. {
18. outfile< 19. } 20. myfile.close(); 21. return 0; 22.} 二、string流 string头文件定义了三个类型来支持内存IO,istringstream向string写入数据,ostringstream从string读取数据,stringstream既可从string读取数据也可向string写数据,就像string是一个IO流一样。 1、istringstream的用法 [cpp] viewplain copy 1.#include "stdafx.h" 2.#include 3.#include 4.#include 5.using namespace std; 6.int _tmain(int argc, _TCHAR* argv[]) 7.{ 8. string str = "I am a boy"; 9. istringstream is(str); 10. string s; 11. while (is >> s) 12. { 13. cout << s << endl; 14. } 15. return 0; 16.} 输出结果为: I am a boy 例: 编写程序,将来自一个文件中的行保存在一个vector [cpp] viewplain copy 1.#include "stdafx.h" 2.#include 3.#include 4.#include 5.#include 6.#include 7.using namespace std; 8.int _tmain(int argc, _TCHAR* argv[]) 9.{ 10. vector 11. ifstream myfile("E: \\hello.txt"); 12. string temp; 13. if (! myfile.is_open()) 14. { 15. cout << "未成功翻开文件" << endl; 16. } 17. while(getline(myfile,temp)) 18. { 19. vec.push_back(temp); 20. } 21. for (auto it = vec.begin(); it ! = vec.end(); it++) 22. { 23. cout << *it << endl; 24. } 25. cout << "-----------------使用istringstream------------------------" << endl; 26. for (auto it = vec.begin(); it ! = vec.end(); it++) 27. { 28. 29. istringstream record(*it); 30. string s; 31. while (record >> s) 32. cout << s << endl; 33. } 34. return 0; 35.} 运行结果如下图: //下述阐述转自 ,程序作者: 管宁 [cpp] viewplain copy 1.#i nclude 2.#i nclude 3.using namespace std; 4.int main() 5.{ 6. istringstream istr; 7. istr.str("1 56.7",); 8. //上述两个过程可以简单写成 istringstream istr("1 56.7");
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 读取 txt 文件