<address id="ousso"></address>
<form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
  1. C語言

    C語言文件的創建與建立

    時間:2025-02-14 21:37:30 C語言 我要投稿

    C語言文件的創建與建立

      c語言中對于文件的創建與建立已經有相當經典且應用相當廣泛的語法了。下面是小編為大家帶來的C語言文件的創建與建立的知識,歡迎閱讀。

      首先是文件的創建:

      # include

      # include

      # include

      using namespace std;

      int main() {

      ofstream outclientfile("clients.dat", ios::out);

      if (!outclientfile) {

      cerr << "file could not be opend" << endl;

      exit(1);

      }

      cout << "enter the account,name,and balance." << endl;

      cout<< "enter end-of-file to end input. ?";

      int account;

      char name[30];

      double balance;

      while (cin >> account >> name >> balance) {

      outclientfile << account << " " << name << " " << balance << endl;

      cout << "?";

      }

      system("pause");

      return 0;

      }

      以下是文件的讀取:

      # include

      # include

      # include

      # include

      # include

      using namespace std;

      void outputline(int, const string, double);

      int main() {

      ifstream inclientfile("clients.dat", ios::in);

      if (!inclientfile) {

      cerr << "file could not be opened" << endl;

      exit(1);

      }

      int account;

      char name[30];

      double balance;

      cout << left << setw(10) << "account" << setw(13) << "name"

      << "balance" << endl<

      while (inclientfile >> account >> name >> balance) {

      outputline(account, name, balance);

      }

      system("pause");

      return 0;

      }

      void outputline(int account, const string name, double balance) {

      cout << left << setw(10) << account << setw(13) << name

      << setw(7) << setprecision(2) << right << balance << endl;

      }

      知識點:以文件的創建為例,我們在頭文件中使用# include包含了ofstream類,并且在主程序中使用類ofstream建立了名為outclientfile對象,并且初始化其構造函數。要注意的是我們在while只是判斷條件的真假,而類outclientfile進行輸入數據,在這里我也有疑問的是?在編譯為什么是出現在輸入數據之前的?這一點以后明白了再找機會說明,或者有知道的小伙伴也可以發消息告知我一下?


    【C語言文件的創建與建立】相關文章:

    c語言—文件的創建與建立01-12

    c語言文件創建與建立05-31

    怎么利用c語言創建excel文件08-13

    C語言文件03-02

    C語言的文件概念07-18

    C語言文件操作的方法03-29

    C語言文件操作函數05-22

    C語言頭文件封裝06-25

    C語言文件操作教程05-11

    <address id="ousso"></address>
    <form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
    1. 日日做夜狠狠爱欧美黑人