ifstream输出中的奇怪字符
Weird characters on the output of an ifstream
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
using namespace std;
int main(){
char a;
cout << "give me the filename: ";
cin >> filename;
ifstream caroll;
caroll.open(filename.c_str());
while (a=caroll.get() && !caroll.eof()){
cout << a << " ";
}
caroll.close();
}
我得到的输出充满了奇怪的字符。它们就像小方块,里面有2个0和2个1
请打开编译器的警告级别。这里有一个bug:
while (a=caroll.get() && !caroll.eof()) {
这被解释为:
while (a = (caroll.get() && !caroll.eof()) ) {
^ ^
你需要给赋值加上括号:
while ((a = caroll.get()) && !caroll.eof() ) {
^ ^
GCC发出警告。
(注意:请提交编译代码,filename
没有在您的示例中声明,当您应该包括string
时,您包括了cstring
)
相关文章:
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 字符的输入/输出操作
- 如何读取单个字符并在输入两个字符序列时输出? 使用 while 循环和C++
- C++输出奇怪的字符而不是数字 (Windows)
- 如何存储 unicode 字符并将其输出到文件?
- C++中附加的字符*的奇怪输出
- 来自无符号字符缓冲区的虚拟输出
- 将未签名的字符写入输出文件
- 在大小为 10 的 char 数组中输入超过 10 个字符的字符串并输出时,它会显示整个数组而不是前 10 个数组.为什
- 如何在C++中正确地将我的语言字符输出到HTML文件中
- x86 上的指针大小和奇怪的字符输出
- 如何修复C++中的随机字符输出?
- Visual Studio-怪异的字符输出
- 需要我当前的字符输出,这是它在C++中的整数值
- 宽字符输出结果
- 将字符输出随机化为文本文件
- C++和printf-奇怪的字符输出
- 显示无符号字符*和有符号字符*输出的结果是否完全相同
- 下一个回文 .奇怪的字符输出
- 不要将宽字符输出到命令提示符