在 C++ 中建立檔案

Jinku Hu 2023年10月12日
  1. 使用 std::fstreamstd::openstd::ios_base::out 在 C++ 中建立檔案
  2. 使用 std::fstreamstd::openstd::ios_base::app 在 C++ 中建立檔案
  3. 使用 std::fstreamfopen 函式在 C++ 中建立檔案
在 C++ 中建立檔案

本文將介紹幾種如何在 C++ 中建立檔案的方法。

使用 std::fstreamstd::openstd::ios_base::out 在 C++ 中建立檔案

建立檔案通常與檔案開啟操作有關,檔案開啟操作本身就是將檔案流結構與磁碟上相應的物理檔案相關聯的操作。通常,檔案建立操作取決於程式開啟檔案的模式。有多種開啟檔案的模式,例如只寫模式,只讀模式,追加模式等。通常,如果給定檔名不存在,則需要寫入檔案的每種模式都旨在建立一個新檔案。因此,我們需要呼叫 std::fstreamopen 內建函式來建立一個新檔案,其名稱作為第一個 string 引數提供。open 的第二個引數指定開啟檔案流的模式,這些模式由語言預先定義(請參見 page)。

#include <fstream>
#include <iostream>

using std::cerr;
using std::cout;
using std::endl;
using std::fstream;
using std::ofstream;
using std::string;

int main() {
  string filename("output.txt");
  fstream output_fstream;

  output_fstream.open(filename, std::ios_base::out);
  if (!output_fstream.is_open()) {
    cerr << "Failed to open " << filename << '\n';
  } else {
    output_fstream << "Maecenas accumsan purus id \norci gravida pellentesque."
                   << endl;
    cerr << "Done Writing!" << endl;
  }

  return EXIT_SUCCESS;
}

輸出:

Done Writing!

使用 std::fstreamstd::openstd::ios_base::app 在 C++ 中建立檔案

或者,我們可以以 std::ios_base::app 表示的追加模式開啟檔案,並在每次寫入時強制將流定位在檔案末尾。這種模式也假設在給定的路徑中不存在新的檔案,就會建立一個新的檔案。請注意,可以使用 is_open 函式驗證成功開啟的檔案流,該函式在與流關聯時返回 true

#include <fstream>
#include <iostream>

using std::cerr;
using std::cout;
using std::endl;
using std::fstream;
using std::ofstream;
using std::string;

int main() {
  string text("Large text stored as a string\n");
  string filename2("output2.txt");
  fstream outfile;

  outfile.open(filename2, std::ios_base::app);
  if (!outfile.is_open()) {
    cerr << "failed to open " << filename2 << '\n';
  } else {
    outfile.write(text.data(), text.size());
    cerr << "Done Writing!" << endl;
  }

  return EXIT_SUCCESS;
}

輸出:

Done Writing!

使用 std::fstreamfopen 函式在 C++ 中建立檔案

建立檔案的另一種方法是利用 C 標準庫介面 fopen,該介面返回 FILE*結構體。該函式有兩個引數:一個字串,該字串指定要開啟的檔案路徑名;另一個字串文字,用於指示開啟方式。定義了六種常見模式:r-當流位於開頭時是隻讀的,w-只寫-在開頭時的內容,a-只寫(追加)-放在末尾的位置檔案,以及三個版本的加號-r+w+a+,其中還包括相反的模式。

#include <fstream>
#include <iostream>

using std::cerr;
using std::cout;
using std::endl;
using std::fstream;
using std::ofstream;
using std::string;

int main() {
  string text("Large text stored as a string\n");
  fstream outfile;

  string filename3("output3.txt");
  FILE *o_file = fopen(filename3.c_str(), "w+");
  if (o_file) {
    fwrite(text.c_str(), 1, text.size(), o_file);
    cerr << "Done Writing!" << endl;
  }

  return EXIT_SUCCESS;
}

輸出:

Done Writing!
作者: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn

相關文章 - C++ Filesystem