C++语言-06-文件操作

C语言文件操作

	C++语言是C语言的超集,是在C语言的基础上增加了面向对象的特性而创造出来的,最初被命名为带类的C。所以C++语言中包含了C语言的思想,如:C++语言中文件操作的原理与步骤与C语言基本相同,请对比C语言中的文件操作,来学习和理解C++中的文件操作。以下是C语言文件操作的Blog连接:

C++语言文件操作

	C++语言中标准库fstream,提供了有关文件操作所需的条件。
  • 与文件操作相关的数据类型

    • ifstream
      • 输入文件流,用于从文件读取信息
      • 使用其对象打开文件时,默认为:ios::in
    • ostream
      • 输出文件流,用于创建文件(若关联的文件不存在)并向文件写入信息
      • 使用其对象打开文件时,默认为:ios::out
    • fstream
      • 文件流,具备ifstream与ostream的功能
      • 使用其对象打开文件时,默认为:ios::in | ios::out
  • 文件的打开模式

    标志 含义
    ios::app 追加模式,将所有写入的内容追加到文件末尾
    ios::ate 文件打开后定位到文件末尾
    ios::in 打开文件用于读取
    ios::out 打开文件用于写入
    ios::trunc 若文件已经存在,其内容将在打开文件之前被截断,即把文件长度设为0(文件内容会被清空)
  • 文件操作的步骤

    • 打开文件,可以使用以下函数打开文件:

      // 使用char类型字符串指定文件名和位置
      void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out)
      // 使用string类型字符串指定文件名和位置
      void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out)
      
    • 操作文件

      • 读取文件

        // 使用流提取运算符 >>,读取到空格时会结束
        // 使用 get() 函数
        basic_istream& get(char_type* __s, streamsize __n);
        // 使用 getline() 函数
        basic_istream& getline(char_type* __s, streamsize __n);
        // 使用 read()
        basic_istream& read (char_type* __s, streamsize __n);
        // 使用 readsome() 函数, 注意函数的返回值类型不是 basic_istream&
        streamsize readsome(char_type* __s, streamsize __n);
        
      • 写入文件

        // 使用流插入运算服务 << 
        // 使用 put() 函数,一次只能写入一个字符
        basic_ostream& put(char_type __c);
        // 使用 write() 函数
        basic_ostream& write(const char_type* __s, streamsize __n);
        
    • 关闭文件

      // 使用 close() 函数
      void close();
      

文件位置重定位

  • 查找方向

    标志 含义
    ios::beg 默认的方向,从流的开头开始定位
    ios::cur 从流的当前位置开始定位
    ios::end 从流的末尾开始定位
  • 文件位置重定位相关的函数

    • 输入流相关

      // __pos 指定位置,查找方向为默认方向:从流的开头开始向后定位。即:定位到流的第 __pos 个字节
      basic_istream& seekg(pos_type __pos);
      // __off 指定偏移量,__dir 指定查找方向。即:按 __dir 指定的查找方向偏移 __off 字节
      basic_istream& seekg(off_type __off, ios_base::seekdir __dir);
      
    • 输出流相关

      // __pos 指定位置,查找方向为默认方向:从流的开头开始向后定位。即:定位到流的第 __pos 个字节
      basic_ostream& seekp(pos_type __pos);
      // __off 指定偏移量,__dir 指定查找方向。即:按 __dir 指定的查找方向偏移 __off 字节
      basic_ostream& seekp(off_type __off, ios_base::seekdir __dir);
      

示例

  • 读取文件

    void readFromFile() {
        // 创建一个字符数组,用于暂存用户输入的数据
        char data[50];
        // 打开文件(以读模式打开文件)
        ifstream inputFile;
        inputFile.open("/Users/mac/Desktop/HelloWorld.txt");
        // 将文件的读指针从开始位置向后移动 6 个字节,即:从文件的第 6 个字节开始读取文件的内容
        inputFile.seekg(6, ios::beg);
        // 读取文件的内容
        cout << "Reading from the file" << endl;
        // 使用 read()函数读取文件内容到 data 中
        inputFile.read(data, 50);
        cout << data << endl;
        // 关闭打开的文件
        inputFile.close();
    }
    
  • 写入文件

    void writeToFile() {
        // 创建一个字符数组,用于暂存从文件读取的数据
        char data[50];
        // 打开文件(以写模式打开文件)
        ofstream outputFile;
        outputFile.open("/Users/mac/Desktop/HelloWorld.txt");
        // 输入要写入文件的内容
        cout << "Writing to the file" << endl;
        cout << "Input a message:" << endl;
        cin.getline(data, 50);
        // 使用 write() 函数,将data数据写入到文件中
        outputFile.write(data, 50);
        // 关闭打开的文件
        outputFile.close();
    }
    
  • main函数

    int main(int argc, const char * argv[]) {
        // 写入文件
        writeToFile();
        // 读取文件
        readFromFile();
        return 0;
    }
    
  • 运行结果如图

posted @ 2016-03-01 09:45  世俗孤岛  阅读(439)  评论(0编辑  收藏  举报