對于文件輸入,C++使用類似于cout的東西。下面來復習一些有關將cout用于控制臺輸出的基本事實,為文件輸入做準備:
● 必須包含頭文件iostream。
● 頭文件iostream定義了一個用處理輸出的ostream類。
● 頭文件iostream聲明了一個名為cout的ostream變量(對象)。
● 必須指明名稱空間std:例如,為引用元素cout和endl,必須使用編譯指令using或前綴std::。
● 可以結合使用cout和操作符“>>”來顯示各種類型的數據。
文件輸出與此極其相似:
● 必須包含頭文件fstream。
● 需要聲明一個或多個ofstream變量(對象),并以自己喜歡的方式對其進行命名,條件是遵守常用的命名規則。
● 必須指明名稱空間std:例如,為引用元素ofstream,必須使用編譯指令using或前綴std::。
● 需要將ofstream對象與文件關聯起來。為此,方法之一是使用open()方法。
● 使用完文件后,應使用close()方法將其關閉。
● 可結合使用ofstream對象和操作符>>來輸出各種類型的數據。
注意,雖然頭文件iostream提供了一個預先定義好的名為cout的ostream對象,但您必須聲明自己的ofstream對象,為其命名,并將其同文件關聯起來。下面演示了如何聲明這種對象。
ofstream outFile; //outFile an ofstream object
ofstream fount; //fout an ofstream object
下面演示了如何將這種對象與特定的文件關聯起來:
outFile.open("fish.txt"); //outFile used to write to the fish.txt file
char filename[50];
cin >> filename; //user specifies a name
fout.open(filename); //fout used to read specified file
注意,方法open()接受一個C-風格字符串作為參數,這可以是一個字面字符串,也可以是存儲在數組中的字符串。
下面演示了如何使用這種對象:
double wt = 125.8;
outFile << wt; //write a number to fish.txt
char line[81] = "Objects are closer than they appear.";
fin << line << endl; //write a line of text
重要的是,聲明一個ofstream對象并將其同文件關聯起來后,便可以像使用cout那樣使用它。所有可用于cout的操作和方法(如<<、enld和setf())都可用于ofstream對象(如前述范例中的outFile和fout)。
總之,使用文件輸出的主要步驟如下:
1. 包含頭文件fstream。
2. 創建一個ofstream對象。
3. 將該ofstream對象同一個文件關聯起來。
4. 就像使用count那樣使用該ofstream對象。
程序清單6.15中的程序演示了這種方法。它要求用戶輸入信息,然后將信息顯示到屏幕上,再將這些信息寫入到文件中。讀者可以使用文本編輯器來查看該輸出文件的內容。
程序清單6.15 outfile.cpp
//outfile.cpp -- writing to a file
#include <iostream>
#include <fstream>
int main() {
using namespace std;
char automobile[50];
int year;
double a_price;
double d_price;
ofstream outFile; //create object for output
outFile.open("carinfo.txt"); //associate with a file
cout << "Enter the make and model of automobile:";
cin.getline(automobile, 50);
cout << "Enter the model year:";
cin >> year;
cout << "Enter the original asking price:";
cin >> a_price;
d_price = 0.913 * a_price;
// display information on screen with count
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;
// now do exact same things using outFile instead of cout
outFile << fixed;//如果輸出的數據是浮點數.則按定點表示法輸出
outFile.precision(2);//按照指定的精度控制浮點數輸出
outFile.setf(ios_base::showpoint);//為輸出的數據強制加小數點以及小數點后
outFile << "Make and model:" << automobile << endl;
outFile << "Year:" << year << endl;
outFile << "Was asking $" << a_price << endl;
outFile << "Now asking $" << d_price << endl;
outFile.close(); //done with file
return 0;
}
該程序的最后一部分與cout部分相同,只是將cout替換為outFile而已。下面是該程序的運行情況:
Enter the make and model of automobile: Flitz Pinata
Enter the model year: 2001
Enter the original asking price :28576
Make and model: Flitz Pinata
Year: 2001
Was asking $28576.00
Now asking $26089.89
屏幕輸出的是使用cout的結果。如果您查看該程序的可執行文件所在的目錄,將看到一個名為carinfo.txt的新文件,其中包含使用outFile生成的輸出。
程序說明
在程序清單6.15的程序中,聲明一個ofstream對象后,便可以使用方法open()來將該對象同一個特定的文件關聯起來:
ofstream outFile; //create object for output
outFile.open("carinfo.txt"); //associate with a file
程序使用完該文件后,應該將其關閉:
outFile.close();
注意,方法close()不需要使用文件名作為參數,這是因為outFile已經同特定的文件關聯起來了。如果您忘記關閉文件,程序正常終止時將自動關閉它。
outFile可使用cout可使用的任何方法。它不但能夠使用操作符<<,還可以使用各種格式化方法,如setf()和precision()。這些方法只影響調用它們的對象。例如,對于不同的對象,可以提供不同的值:
cout.precision(2); //use a precision of 2 for the display
outFile.porecision(4); //use a precision of 4 for file output
讀者需要記住的重點是,創建好ofstream對象(如outFile)后,便可以像使用cout那樣使用它。
回到open()方法:
outFile.open("carinfo.txt");
在這里,該程序運行之前,文件carinfo.txt并不存在。在這種情況下,方法open()將新建一個名為carinfo.txt的文件。如果在此運行該程序,文件carinfo.txt將存在,此時情況將如何呢?默認情況下,open()將首先截斷該文件,即將其長度截短到零——丟其原有的內容,然后將新的輸出加入到該文件中。第17章將介紹如何修改這種默認行為。
警告:打開已有的文件,以接受輸出時,默認將它其長度截短為零,因此原來的內容將丟失。
打開文件用于接受輸入時可能失敗。例如,指定的文件可能已存在,但禁止對其進行訪問。因此細心的程序員將檢查打開文件的操作是否成功,這將在下一個例子中介紹。