文件的讀寫(cpp)


c++中要進行文件的讀入,首先要包含一個頭文件 fstream

輸出到文件

為打開一個可供輸出的文件需要定義一個ofstream 對象並將文件名傳入:

std::ofstream out("out.txt");

在不做任何其他操作的情況下,如果該文件不存在就會創建一個相應文件,如果存在就會打開並將原來文件中的信息全部覆蓋。如果想要不覆蓋原文件而僅僅是在文件的末尾加上要輸出的信息,只需要在定義ofstream對象的時候傳入第二個參數std::ios_base::app以打開追加模式(append_mode):

std::ofstream out_with_append("out.txt", std::ios_base::app);

此外由於文件可能會打開失敗,所以需要用if判斷文件是否打開成功。

#include <fstream>
#include <iostream>

int main () {
  //輸入到文件
  std::ofstream out("out.txt");
  if (!out) {
    std::cerr << "error in out" << std::endl;
  } else {
    std::cerr << "successfully open out file" << std::endl;
    out << "Hello world!" << std::endl;
  }
  std::ofstream out_with_append("out.txt", std::ios_base::app);
  if (!out_with_append) {
    std::cerr << "error in out_with_append" << std::endl;
  } else {
    std::cerr << "successfully open out_with_append file" << std::endl;
    out_with_append << "Hello world again!" << std::endl;
  }
  return 0;
}

這其中std::endl是事先定義好的操作符,由iostream library 提供,他的作用是插入一個換行符並清除輸出緩沖區(output buffer)的內容。

從文件中讀入

要打開一個可供讀入的文件,需要先定義一個ifstream對象並將供於讀入的文件傳入:

std::ifstream in("out.txt");

同樣的文件依然可能會無法正常打開,所以也需要用if判斷一下文件是否打開成功。

下面在上面代碼的基礎上增添ifstream的功能

#include <fstream>
#include <iostream>
#include <string>

int main () {
    //輸出到文件
    std::ofstream out("out.txt");
    if (!out) {
        std::cerr << "error in out" << std::endl;
    } else {
        std::cerr << "successfully open out file" << std::endl;
        out << "Hello world!" << std::endl;
    }
    std::ofstream out_with_append("out.txt", std::ios_base::app);
    if (!out_with_append) {
        std::cerr << "error in out_with_append" << std::endl;
    } else {
        std::cerr << "successfully open out_with_append file" << std::endl;
        out_with_append << "Hello world again!" << std::endl;
    }
    //從文件中讀入
    std::ifstream in("out.txt");
    if (!in) {
        std::cerr << "error in in" << std::endl;
    } else {
        std::cerr << "successfully open in file" << std::endl;
        std::string s;
        while (in >> s) {
            std::cout << s;
        }
    }
    return 0;
}

這里控制台的全部輸出內容為

successfully open out file
successfully open out_with_append file
successfully open in file
Helloworld!Helloworldagain!
Process finished with exit code 0

首先注意到這里在in >> s外面套了一層while,原因是每次讀入在沒有讀到文件末尾的時候 in >> s會返回一個 in,而當讀到文件末尾的時候會返回false,因此可以在while的循環表達式中作為結束的標志。

其次會發現輸出的內容和輸入的內容不一樣,原因我引用了下面一段解釋

因為istream_iterator的自增的副作用等價於相應流通過>>操作符讀一個T類型的數據,而istream通過>>操作符讀一個字符類型(這里就是char)的數據會跳過前面的空白字符

這里的空白字符不僅包括了空格,還包括換行符。

ofstream一樣,ifstream同樣也有追加模式。為了以追加模式打開文件需要傳入第二個參數std::ios::bash::in|std::ios::bash::app :

std::ifstream in("out.txt", std::ios_base::in|std::ios_base::app);

這里需要注意的是以追加模式打開文件會默認在文件的末尾,如果沒有重新先定位就讀取文件內容,那么就會立即遇到文件結束(EOF)的情況。seekg()可以將in重新定位到文件的起始處。

追加模式作用?

#include <fstream>
#include <iostream>
#include <string>

int main () {
    //輸出到文件
    std::ofstream out("out.txt");
    if (!out) {
        std::cerr << "error in out" << std::endl;
    } else {
        std::cerr << "successfully open out file" << std::endl;
        out << "Hello world!" << std::endl;
    }
    std::ofstream out_with_append("out.txt", std::ios_base::app);
    if (!out_with_append) {
        std::cerr << "error in out_with_append" << std::endl;
    } else {
        std::cerr << "successfully open out_with_append file" << std::endl;
        out_with_append << "Hello world again!" << std::endl;
    }
    //從文件中讀入
    std::ifstream in("out.txt", std::ios_base::in|std::ios_base::app);
    in.seekg(0);
    if (!in) {
        std::cerr << "error in in" << std::endl;
    } else {
        std::cerr << "successfully open in file" << std::endl;
        std::string s;
        while (in >> s) {
            std::cout << s;
        }
        std::cout << std::endl;
        out_with_append << "append text" << std::endl;
        in >> s;
        std::cout << s << std::endl;
    }
    return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM