>">
当前位置:   article > 正文

C++对文件的操作(新手入门)_c+中file>>

c+中file>>

引文:

输出流:ostream (output stream)

输出流:istream (input stream)

输入输出流:iostream

 写操作(输出)的文件类 :ofstream

读操作(输入)的文件类:ifstream

 可同时读写操作的文件类:fstream (file stream)

 

理解例子:

 

ofstream examplefile("example.txt");//声明类example并与创建的文件example.txt联系

其中examplefile是ofstream的对象,对象(examplefile)第一个操作通常与一个真正的文件(example.txt)联系起来,改文件由一个流对象(该例子为examplefile)来表示(这些类的一个实例),对改对象进行的操作就是对该对象联系的文件操作。

examplefile.is_open()//检查一个文件(examplefile对象所联系的文件)是否被顺利打开

bool is_open();

examplefile << "This is a line.\n";

examplefile << "This is another line.\n";

examplefile.close();

写入操作和关闭文件。close(),它负责将缓存中的数据排放出来并关闭文件。使用之后原对象可以访问其他文件。

ps:为防止流对象被销毁时还联系着打开的文件,析构函数(destructor)将会自动调用关闭函数close

 

代码示例:

  1. //建立一个文件并写入内容
  2. #include <fstream>
  3. using namespace std;
  4. int main()
  5. {
  6. ofstream examplefile("example.txt");
  7. if (examplefile.is_open())
  8. {
  9. examplefile << "This is a line.\n";
  10. examplefile << "This is another line.\n";
  11. examplefile.close();
  12. }
  13. return 0;
  14. }

 

 

 

理解例子:

补充:exit(): 头文件:“stdlib.h  或写 <cstdlib>

功 能: 关闭所有文件,终止正在执行的进程。

exit(0)表示正常退出,exit(x)(x不为0)都表示异常退出,这个x是返回给操作系统(包括UNIX,Linux,和MS DOS)的,以供其他程序使用

 

examplefile.eof();

eof ,它是ifstream 从类 ios 中继承过来的,当到达文件末尾时返回true

 

examplefile.getline(buffer,100);

getline,读取一行字符,直到遇见换行符。getline具有返回值:成功:返回读取的字节数  失败:返回-1。  

代码示例:

  1. //对文件的读取示例
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cstdlib>
  5. using namespace std;
  6. int main ()
  7. {
  8. char buffer[256]; //定义一个数组,用来存放字符
  9. ifstream examplefile("example.txt"); //声明一个对象与要读的文件联系
  10. if (! examplefile.is_open()) //
  11. {
  12. cout << "Error opening file"; exit (1);
  13. }
  14. while (!examplefile.eof())
  15. {
  16. examplefile.getline(buffer,100);
  17. cout<<buffer<< endl;
  18. }
  19. return 0;
  20. }

 

状态标识符的验证

 

 

bad() 如果在读写过程中出错,返回 true 。例如:当我们要对一个不是打开为写状态的文件进行写入时,或者我们要写入的设备没有剩余空间的时候。
fail() 除了与bad() 同样的情况下会返回 true 以外,加上格式错误时也返回true ,例如当想要读入一个整数,而获得了一个字母的时候。
eof() 如果读文件到达文件末尾,返回true。(上文所用)
good() 这是最通用的:如果调用以上任何一个函数返回true 的话,此函数返回 false 。
tellg() 和 tellp() 这两个成员函数不用传入参数,返回pos_type 类型的值(根据ANSI-C++ 标准) ,就是一个整数,代表当前get 流指针的位置 (用tellg) 或 put 流指针的位置(用tellp).
seekg() 和seekp() 这对函数分别用来改变流指针get 和put的位置。两个函数都被重载为两种不同的原型:

 

 

代码示例:

  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int main ()
  5. {
  6. const char * filename = "example.txt";
  7. long l,m;
  8. ifstream file(filename, ios::in|ios::binary);
  9. l = file.tellg();
  10. file.seekg(0, ios::end);
  11. m = file.tellg();
  12. file.close();
  13. cout <<"size of "<< filename;
  14. cout <<" is "<< (m-l)<<" bytes.\n";
  15. return 0;
  16. }

 

 

 

首先要知道以下标识符:

ios::out 为输出(写)而打开文件 

ios::ate 初始位置:文件尾 

ios::app 所有输出附加在文件末尾 

ios::trunc 如果文件已存在则先删除该文件 

ios::binary 二进制方式

ios::in 为输入(读)而打开文件 

ios::end  流尾

 

上边代码利用文件二进制首尾位置,做差得出文件大小(二进制长度)。

 

 

read和write函数

分别为istream和ostream的成员函数,前者被ifstream所继承,后者被ofstream所继承。

 

代码示例:

  1. #include <fstream>
  2. #include <iostream>
  3. using namespace std;
  4. int main ()
  5. {
  6. const char * filename = "example.txt";
  7. char * buffer;//buffer是一块内存地址,用来存储或读取数据
  8. long size;//size 是一个整数值,表示要从缓存(buffer)中读出或写入的字符数
  9. ifstream file(filename, ios::in|ios::binary|ios::ate);
  10. size = file.tellg();
  11. file.seekg(0, ios::beg);
  12. buffer = new char [size];
  13. file.read(buffer, size);
  14. file.close();
  15. cout <<"the complete file is in a buffer";
  16. delete[] buffer;
  17. return 0;
  18. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/852567
推荐阅读
相关标签
  

闽ICP备14008679号