2010-07-28 47 views
-1

例如我們已經在C++中創建了文件如何在此文件中編寫內容然後在屏幕上輸出?對C++中的文件進行的操作

+1

http://www.cplusplus.com/doc/tutorial/files/你有GOOGLE嗎? – DumbCoder 2010-07-28 11:08:51

+0

「如何在此文件中寫入內容,然後在屏幕上輸出?」簡單。只需使用標準庫提供的功能即可。 – SigTerm 2010-07-28 11:13:11

+0

http://www.google.com/search?q=file+reading+c%2B%2B – liaK 2010-07-28 11:16:00

回答

0

摘自here

寫作:

#include <iostream> 
#include <fstream> 
using namespace std; 

int main() { 
    ofstream myfile; 
    myfile.open ("example.txt"); 
    myfile << "Writing this to a file.\n"; 
    myfile.close(); 
    return 0; 
} 

閱讀:

#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

int main() { 
    string line; 
    ifstream myfile("example.txt"); 
    while(getline(myfile,line)) { 
     cout << line << endl; 
    } 
    myfile.close(); 
    return 0; 
} 

你真的應該使用谷歌。

1

閱讀this然後回來問你是否有更具體的問題,謝謝。

+0

我有問題沒有人給我輸出爲什麼? – 2010-07-28 12:47:42

+0

因爲你問的問題是一個非常籠統的問題,說實話,這個問題很簡單。雖然我們不要求你花費數小時搜索答案,但對於這樣的事情,「C++寫入內容文件」的快速谷歌會讓你在我的答案中提供的鏈接,它有兩個代碼示例,也是一個很好的教程在C++中進行文件讀取/寫入。下面的vtorhonen實際上引用了該鏈接中的示例,這樣您甚至不必點擊它。如果你還在掙扎,我可以在你的答案中寫下一個例子。 – Stephen 2010-07-28 12:56:09