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()
{
ofstream output;
ifstream input;
string firstname, lastname;
output.open("LastName.txt");
input.open("FirstName.txt");
cout << "Processing Data..." << endl;
input >> firstname >> lastname;
cout << firstname << lastname << endl;
output << lastname << firstname;
cout << lastname << firstname << endl;
input >> firstname >> lastname;
cout << firstname << lastname << endl;
output << lastname << firstname;
cout << lastname << firstname << endl;
input >> firstname >> lastname;
cout << firstname << lastname << endl;
output << lastname << firstname;
cout << lastname << firstname << endl;
input.close();
output.close();
cin.get();
cin.get();
return 0;
}
我的程序需要在名称之间留有空格,即使文本文档中有空格,也不会读取空格。有没有人知道我应该怎么做以便阅读空格?
答案 0 :(得分:2)
我猜你想在输出中看到空格,但你没有得到它们。这让你觉得空间没有被阅读。事实是,正在读取空白区域,但在使用时会被丢弃:
input >> firstname >> lastname;
您需要将创建输出的行更改为:
cout << firstname << " " << lastname << endl;
output << lastname << " " << firstname << endl;