# C++ 讀檔&寫檔筆記 宣告fstream **每次寫完絕對要記得關檔( f.close() )** ## 寫檔(ofstream) ```C++ ofstream outputFile; string s; outputFile.open("Friends.txt",ios::app); cin >> s; outputFile << s << endl; outputFile.close(); ``` 寫檔時也可使用iomanip的各種做法,如 ``` outputFile << showpoint << fixed << setprecision(2); ``` ## 讀檔(ifstream) ```C++ ifstream inputFile; string s; inputFile.open("Friends.txt"); if (inputFile){ while(inputFile >> s) cout << s << endl; inputFile.close(); } ``` 也可以用getline方式讀整行 ``` string s; getline(inputFile, s); ``` ## 可寫也可讀(fstream) 可在open的第二個參數控制權限 ```C++ #include<fstream> fstream inOutFile; string word; inOutFile.open("inout.txt"); if (inOutFIle.fail()) { cout << "The file was not found." << endl; return 1; } // Read and print every word already in the file while (inOutFile >> word) cout << word << endl; // Clear end of file flag to allow additional file operations inOutFile.clear(); // Write a word to the file and close the file inOutFile << "Hello" << endl; inOutFile.close(); ``` ## open函式 ``` ofstream outputFile; outputFile.open(檔名,模式) ``` ### 第一個參數 第一個參數為檔名,若無此檔則會新開一個 可以直接打,也可以用輸入檔名的方式用變數傳入,但要注意,只能用C的字元串,不可用C++的string,需用stringObject.c_str() ```C++ ifstream inputFile; string filename; inputFile.open(filename.c_str()); ``` ### 第二個參數 第二個參數決定要覆寫、從後面加或是單純讀取。 * ios::out :**預設設置**,覆寫 * ios::app :從檔案最後加入 * ios::in : 單純讀取 * ios::out : 單純寫檔 * ios::trunc : file contents are discarded ## Error State Bits * ios::eofbit : set when end of file detected * ios::failbit : set when operation failed * ios::hardfail set when an irrecoverable error occurred * ios::badbit set when invalid operation attempted * ios::goodbit set when no other bits are set * eof() true if eofbit set, false otherwise * fail() true if failbit or hardfail set, false otherwise * bad() true if badbit set, false otherwise * good() true if goodbit set, false otherwise * clear() clear all flags (no arguments), or clear a specific flag ```C++ void showState(fstream &file){ cout << "File Status:\n"; cout << " eof bit: " << file.eof() << endl; cout << " fail bit: " << file.fail() << endl; cout << " bad bit: " << file.bad() << endl; cout << " good bit: " << file.good() << endl; file.clear(); // Clear any bad bits. } ``` ## 輸入(讀檔)方法 ### getline getline(字串流,傳入的字串,讀取結束的符號,若未寫則默認為\n) ```C++ string input; fstream dataFile("addresses.txt", ios::in); getline(dataFile, input, '$'); ``` ## get 讀入單一字元,不會忽略任何符號,讀入後指標向下一位置移動 ```C++ //the content of file: abc char ch = inFile.get(); cout << ch; ch = inFile.get(); cout << ch; ``` ``` 輸出 :ab ``` ## peek 讀入單一字元,不會忽略任何符號,讀入後指標**不移動** ```C++ //the content of file: abc char ch = inFile.peek(); cout << ch; ch = inFile.get(); cout << ch; ``` ``` 輸出 :aa ``` ## seekg seekg(offset, place); seekp用於打開要進行輸出的文件,而 seekg 則適用於打開用於輸入的文件 ![](https://hackmd.io/_uploads/SyKBAuhP2.png) ## Binary file ```C++ file.open("stuff.dat", ios::out | ios::binary); write(char *addressOfBuffer, int numberOfBytes); ``` ``` double dl = 45.9; double dArray[3] = { 12.3, 45.8, 19.0 }; ofstream outFile("stuff.dat", ios::binary); outFile.write(reinterpret_cast<char *>(&dl), sizeof(d1)); outFile.write(reinterpret_cast<char *>(dArray),sizeOf(dArray)); char ch = 'X'; char charArray[5] = "Hello"; outFile.write(&ch, sizeof(ch)); outFile.write(charArray, sizeof(charArray)); ```