日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++中簡單的文本文件輸入/輸出示例詳解_C 語言

作者:francis_xd ? 更新時間: 2022-03-07 編程語言

為了便于理解,我們把cout 用于控制臺輸出時的一些情況和寫入到文本文件的情況進行類比:

cout 控制臺輸出

包含頭文件 iostream

頭文件 iostream 定義了一個 ostream 類用于處理輸出

頭文件 iostream 聲明了一個名為 cout 的 ostream 對象

指明名稱空間 std

可以結合使用cout和運算符<<來顯示各種類型的數據

文本文件輸出(寫入到文本文件)

包含文件頭 fstream

頭文件 fstream 定義了一個ofstream 類用于處理輸出

聲明一個或多個 ofstream 對象,可以自由命名

指明名稱空間 std

把 3 中聲明的 ofstream 對象與文件關聯起來,比如使用 open()方法

使用完文件后,需要使用 close()方法將其關閉

還可以結合 ofstream 對象和運算符<<來輸出各種類型的數據

注意:cout 控制臺輸入輸出中,頭文件 iostream 聲明了一個名為 cout 的 ostream 對象,無需自己手動聲明;文件輸出中,我們必須自己動手聲明一個 ofstream 對象,為其命名,將其同文件關聯起來。請看下面的例子:

#include<fstream>
ofstream OutFile;   //聲明一個 ofstream 對象
OutFile.open("study.txt");  //將OF與“study.txt”文件關聯起來

下面請看一個完整的程序,用戶輸入信息,將信息顯示到屏幕上,再將這些信息寫入到文本文件中:

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    char name[20];
    double height;
    double weight;
    ofstream outFile;//創建了一個ofstream 對象
    outFile.open("information.txt");//outFile 與一個文本文件關聯
    cout<<"Enter name: ";
    cin.getline(automobile, 20);
    cout<<"Enter height: ";
    cin>>year;
    cout<<"Enter weight: ";
    cin>>weight;

    // cout 控制臺輸出前面輸入的信息
    cout<<fixed;
    cout.precision(2);
    cout.setf(ios_base::showpoint);
    cout<<"Make and Model: "<<automobile<<endl;
    cout<<"Year: "<<year<<endl;
    cout<<"Was asking $"<<a_price<<endl;
    cout<<"Now asking $"<<d_price<<endl;

    // outFile 把信息寫入到文本文件
    outFile<<fixed;    //小數點格式顯示double
    outFile.precision(2);    //設置精度
    outFile.setf(ios_base::showpoint);    //強制顯示小數點后的零
    outFile<<"Name: "<<name<<endl;
    outFile<<"Height: "<<height<<endl;
    outFile<<"Weight: "<<weight<<endl;
    outFile.close();    //使用完文本文件后要用close()方法將其關閉
    return 0;
}

接下來,我們再把cin 用于控制臺輸入時的一些情況和讀取文本文件的情況進行類比:

cin 控制臺輸出

包含頭文件 iostream

頭文件 iostream 定義了一個 istream 類用于處理輸出

頭文件 iostream 聲明了一個名為 cin 的 istream 對象

指明名稱空間 std

可以結合使用cin和運算符>>來輸入各種類型的數據

文本文件輸入(讀取文本文件)

包含文件頭 fstream

頭文件 fstream 定義了一個ifstream 類用于處理輸出

聲明一個或多個 ifstream 對象,可以自由命名

指明名稱空間 std

把 3 中聲明的 ifstream 對象與文件關聯起來,比如使用 open()方法

使用完文件后,需要使用 close()方法將其關閉

還可以結合 ifstream 對象和運算符>>來讀取各種類型的數據

可以用 cin 和 get() 方法來讀取一個字符,或用 cin 和 getline() 方法來讀取一行字符

可以結合使用 ifstream 和 eof()、fail()方法來判斷輸入是否成功

如果試圖打開一個不存在的文件用于輸入將會導致后面使用 ifstream 對象讀取數據時發生錯誤,因此用戶需要使用 is_open() 方法檢查文件是否被成功打開,如下:

#include <fstream>
#include <cstdlib>
ifstream InFile;
InFile.open("information.txt");
if(!Infile.is_open()){
    exit(EXIT_FAILURE);
}

如果文件被成功打開, is_open() 方法將返回 true , exit()的原型在頭文件 cstdlib 中被定義,exit(EXIT_FAILURE) 用于終止程序。

下面請看一個完整的程序,用戶打開指定的文本文件,讀取文件中的 double 類型數據,并在控制臺輸出所讀取數據的數目、總和以及平均數:

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int SIZE = 60;
int main()
{
    char fileName[SIZE];
    ifstream InFile;
    cout<<"Enter the name of data file: ";
    cin.getline(fileName, SIZE);
    cout<<fileName<<endl;
    InFile.open(fileName);
    if(!InFile.is_open()){
        cout<<"Could not open the file "<< fileName <<endl;
        cout<<"Program terminating.\n";
        exit(EXIT_FAILURE);
    }
    double value;
    double sum = 0.0;
    int count = 0;
    InFile >> value;
    while(InFile.good()){      
        ++count;
        sum += value;
        InFile >> value;
    }
    if (InFile.eof())
        cout<<"End of file reached:\n";
    else if (InFile.fail())
        cout<<"Input terminated by data mismatch.\n";
    else
        cout<<"Input terminated for unknown reason.\n";
    if(count == 0)
        cout<<"No data processed.\n";
    else{
        cout<<"Items read: "<<count<<endl;
        cout<<"Sum: "<<sum<<endl;
        cout<<"Average: "<<sum/count<<endl;
    }
    InFile.close();
    return 0;
}

總結

原文鏈接:https://blog.csdn.net/francis_xd/article/details/78347595

欄目分類
最近更新