2013-06-25 148 views
-1

該怎麼辦?當你試圖回答這個問題的時候,試着更具體的noob。 沒有額外的細節。我得到一個錯誤C++文件處理錯誤

no match for operator<< in theFileIn<<number 
#include<iostream> 
#include<fstream> 

using namespace std; 

int main() 
{ 
int number; 
string name; 

ofstream theFileOut; 
ifstream theFileIn; 

theFileOut.open("Sansa.txt"); 
cout << "Enter the number and the name"<<endl; 

while(cin>> number>> name) 
{ 
    theFileOut<<number<<" "<<name<<endl; 
} 

theFileOut.close(); 
theFileIn.open("sansa.txt"); 

while(theFileIn>>number>>name) 
{ 
    theFileIn << number<<" "<<name<<endl; 
} 

return 0; 
} 
+2

我認爲這至少是今天發佈的關於文件輸入\輸出的第三個問題。請首先至少研究一下你的話題,因爲如果你自己至少做了一些研究,那麼你的問題都不可能找到答案。 –

+0

您能複製粘貼您收到的確切錯誤信息嗎? – quandrei

回答

1

這裏是出了什麼問題:

theFileIn<<number<<" "<<name<<endl; 

你需要切換theFileOut別的東西,最好是cout

+0

我不認爲'theFileOut'有任何問題。 OP從'cin'中讀取'number'和'name'並將其寫入文件,這是正確的。然後OP試圖從不同的文件中讀取(可能的文件名錯誤,我猜OP意味着剛剛創建的文件),並在屏幕上輸出讀取內容,這一步有錯誤。 – taocp

2

問題是在這裏:

theFileIn << number<<" "<<name<<endl; 

theFileInifstream對象,你不能用ifstream的對象使用<<。你大概的意思:

cout << number<<" "<<name<<endl; 
+0

謝謝taocp,解釋了一切 –

+0

@ Noob.Alone.Programmer不客氣。嘗試瞭解'<<' and '>>'之間的區別,並閱讀關於C++ FILE I/O的一些教程。 – taocp

+0

打敗我吧! @ Noob.Alone.Programmer不要忘記接受答案! – quandrei