gpt4 book ai didi

c++ - 读取输入文件时不打印空格

转载 作者:行者123 更新时间:2023-11-30 01:29:00 25 4
gpt4 key购买 nike

我正在尝试读取输入文件的每个“字符”并写入输出文件,直到找到“?”作为文件的结尾。除了单词之间的空格外,每个字符都写在输出文件中。我不知道这段代码有什么问题??

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile("in.txt");
ofstream outfile("out.txt");
char ch;
infile >> ch;
while(ch != '?')
{
outfile<<ch;
infile >> ch;
}
}

最佳答案

尝试使用 noskipws正在阅读...

infile >> noskipws >> ch;

noskipws 告诉输入流不要跳过默认情况下的空格。

关于c++ - 读取输入文件时不打印空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6656336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com