我的输入文件中没有读取 C++ 空格?

标签 c++

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/

相关文章:

c++ - 无效替换*

c++ - Matlab绘图功能与Qt C++程序

c++ - 从另一个文件访问 C++ 中的 extern "C"变量

模板构造函数中的 C++ 链接器错误 : "unresolved external symbol"

python - 获取字符串中不同索引数量所需的快速方法

c++ - 在 C++ 中将非常量值传递给模板

c++ - Visual Studio 2010 x64 中 map 的运行时错误

c++ - 为什么单例实例必须在下面的代码中初始化

c++ - 在派生类中实现插槽 - qt4/c++

C#:Blowfish 加密单个双字