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;
}
我的程序要求名称之间有空格,即使我的文本文档中有空格,也不会读取这些空格。有谁知道我应该怎么做才能读取这些空格?
最佳答案
我猜您希望在输出中看到空格,但您没有得到它们。这使您认为没有读取空格。事实是,当您使用以下内容时,空白区域会被读取但会被丢弃:
input >> firstname >> lastname;
您需要将创建输出的行更改为:
cout << firstname << " " << lastname << endl;
output << lastname << " " << firstname << endl;
关于我的输入文件中没有读取 C++ 空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33049843/