2015-10-10 19 views
0

C++ Spaces沒有在我的輸入文件中被讀取?

Input txt file: 
 

 
Joe Smith 
 
Mary Jones 
 
Hamid Namdar 
 

 
Desired Output Txt file: 
 

 
Smith Joe 
 
Jones Mary 
 
Namdar Hamid 
 

 
Output file I receive: 
 

 
SmitJoeJonesMaryNamdarHamid

#include <iostream> 
 
#include <string> 
 
#include <fstream> 
 

 
using namespace std; 
 

 
int main() 
 
{ 
 
\t ofstream output; 
 
\t ifstream input; 
 
\t string firstname, lastname; 
 

 
\t output.open("LastName.txt"); 
 
\t input.open("FirstName.txt"); 
 

 
\t cout << "Processing Data..." << endl; 
 

 
\t input >> firstname >> lastname; 
 

 
\t cout << firstname << lastname << endl; 
 

 
\t output << lastname << firstname; 
 

 
\t cout << lastname << firstname << endl; 
 

 
\t input >> firstname >> lastname; 
 

 
\t cout << firstname << lastname << endl; 
 

 
\t output << lastname << firstname; 
 

 
\t cout << lastname << firstname << endl; 
 

 
\t input >> firstname >> lastname; 
 

 
\t cout << firstname << lastname << endl; 
 

 
\t output << lastname << firstname; 
 

 
\t cout << lastname << firstname << endl; 
 

 

 
\t input.close(); 
 
\t output.close(); 
 

 
\t cin.get(); 
 
\t cin.get(); 
 

 
\t return 0; 
 

 
}

我的程序需要有名字之間的空間,即使是我的文本文檔中的空間,不被讀取的空間。有沒有人對我應該做什麼有一個想法,以便讀取空間?

+0

請張貼輸入文件中,預期產出和觀察到的產出。 –

回答

2

我猜你想在你的輸出中看到空格,但你沒有得到它們。這讓你覺得空間不被閱讀。事實是,白色的空間被讀出,但被丟棄,當您使用:

input >> firstname >> lastname; 

您需要更改創建輸出到行:

cout << firstname << " " << lastname << endl; 
output << lastname << " " << firstname << endl;