1. 程式人生 > >C++_寫入到文本文件中

C++_寫入到文本文件中

文件中 讀取文本 isp pac 換行 代碼 ++ 發生 結合

#-----------------寫入到文本文件中----------------#
/*
01)包含頭文件fstream
02)創建一個ofstream對象,名字可以任意取
03)將該ofstream對象和一個文件關聯起來,方法之一就是用open()方法
04)就可以像使用cout一樣去使用該ofstream對象了
05)必須知名名稱空間std,例如,為引用元素ofstream,必須使用編譯指令using或前綴std::
06)使用完文件後,應使用方法close()將其關閉
*/

技術分享圖片
 1 #include <iostream>
 2 #include <fstream>  //for file I/O  01) 包含頭文件
3 4 int main() 5 { 6 using namespace std; 7 8 char automobile[50]; 9 int year; 10 double a_price; 11 double b_price; 12 13 ofstream outFile; // 02) 創建一個ofstream對象 14 outFile.open("carinfo.txt"); //03) 將該ofstream對象(outFile)和一個文件關聯起來, 15 //
此後就可以像使用cout一樣使用outFlie 16 cout << "Enter the make and model of automobile:"; 17 cin.getline(automobile, 50); 18 cout << "Enter the model year: "; 19 cin >> year; 20 cout << "Enter the original asking price: "; 21 cin >> a_price; 22 b_price = 0.913
* a_price; 23 24 //display infomation on screen with cout 25 cout << fixed; //使用一般方式輸出浮點數,而不是可科學計數法 26 cout.precision(2); //設置2為浮點數的精度值 27 cout.setf(ios_base::showpoint); //顯示小數點後面的0 28 cout << "Make the model: " << automobile << endl; 29 cout << "Year: " << year << endl; 30 cout << "was asking $" << a_price << endl; 31 cout << "Now asking $" << b_price << endl; 32 33 //Now do exact same things using outFile instead of cout 34 //但是outFile將cout在屏幕上顯示的內容寫入到了carinfo.txt文件中 35 //如果原來沒有carinfo.txt文件,那麽會自動創建 36 //該代碼將文件創建在了和main.cpp在同一個文件夾中,但是在編譯環境中沒有顯示出該文件 37 //如果已存在該文件,那麽會默認情況下會覆蓋掉文件中所有的內容 38 outFile << fixed; //使用一般方式輸出浮點數,而不是可科學計數法 39 outFile.precision(2); //設置2為浮點數的精度值 40 outFile.setf(ios_base::showpoint); //顯示小數點後面的0 41 outFile << "Making and model: " << automobile << endl; 42 outFile << "Year: " << year << endl; 43 outFile << "was asking $" << a_price << endl; 44 outFile << "Now asking $" << b_price << endl; 45 outFile.close(); //關閉文本文件 46 47 system("pause"); 48 49 return 0; 50 }
寫文件

執行結果會在和main.cpp在同一個文件夾下創建一個carinfo.txt文件

其內容為:

技術分享圖片

cout執行結果為:

技術分享圖片

讀取文本文件

/*
01)包含頭文件fstream
02)創建一個ifstream對象,名字可以任意取
03)將該ifstream對象和一個文件關聯起來,方法之一就是用open()方法
04)就可以像使用cin一樣去使用該ifstream對象了
05)必須知名名稱空間std,例如,為引用元素ifstream,必須使用編譯指令using或前綴std::
06)使用完文件後,應使用方法close()將其關閉
07)可以使用ifstream對象和get()方法來獲取一個字符
08)可以使用ifstream對象和getline()方法來獲取一行字符
09)可以結合ifstream對象和eof()、fail()等方法來判斷輸入是否成功
*/

技術分享圖片
 1 #include <iostream>
 2 #include <fstream>
 3 #include <cstdlib>  //for exit()
 4 
 5 const int SIZE = 60;
 6 
 7 int main()
 8 {
 9     using namespace std;
10     char filename[SIZE];  //剛剛使用size的小寫,顯示size不明確,現在用大寫就沒問題了
11     ifstream inFile;  //02) 創建一個ifstream對象ifFile
12     cout << "Enter name of a data file: ";
13     cin.getline(filename, SIZE);
14     inFile.open(filename);  //03) 將該ofstream對象(outFile)和一個文件關聯起來,
15                            //此後就可以像使用cout一樣使用outFlie
16     if (!inFile.is_open())  //判斷文件是否打開成功,如果打開成功,則inFile.is_open()返回true;否則返回false
17     {
18         cout << "Could not open the file " << filename << endl;
19         cout << "Program terminating" << endl;
20         exit(EXIT_FAILURE);  //退出程序
21     }
22     double value;
23     double sum = 0.0;
24     int count = 0;
25 
26     inFile >> value;  //從文本文件中得到第一個數字,文件中的字符如果和value的類型不一樣,下面的fail()方法就會返回true
27     while (inFile.good())  //1)good()方法指出最後一次讀取輸入是否成功,若成功,則返回true;否則,返回false
28     {                     //2)下面的eof()方法和fail()方法用於判斷到底是因何錯誤導致讀入數據發生錯誤
29                          //3)也可以用while(inFile>>value)來代替,在正確輸入的情況下,inFile>>value返回true,否則返回false
30         ++count;  //對讀取的次數進行計數,以便於接下來計算平均數
31         sum = sum + value;  //進行文件中數據的累加
32         inFile >> value; //進行下一次的讀取,並賦值給vlaue
33     }
34     if (inFile.eof())  //判斷是否讀到了文件的末尾,如果到了末尾,則eof()返回true
35         cout << "End of the file reached" << endl;
36     else if (inFile.fail())  //判斷是否讀到了和value類型不一樣的字符,如果有,則fail()返回true
37         cout << "Input terminated by data mismatch" << endl;
38     else
39         cout << "Input terminated by unknow reason" << endl;
40     if (count == 0)  //判斷文件中是否有數字
41         cout << "No data readed" << endl;
42     else
43     {
44         cout << "Items read: " << count << endl;
45         cout << "Sum: " << sum << endl;
46         cout << "Average: " << sum / count << endl;
47     }
48     inFile.close();
49     system("pause");
50     return 0;
51 }
讀文本文件

執行結果:

技術分享圖片

但是最開始的時候在最後一個字符中沒有加換行符,導致最後一個字符沒有被讀入!!

技術分享圖片

C++_寫入到文本文件中