2011-07-23 158 views
-1
#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的小广场。ifstream输出上奇怪的字符

+0

这输出看起来像是一个调试器会给。 –

+0

输出正常,并在调试模式下预期(您没有提及 - 你应该有!)。另外,什么是所有的空白? –

+0

我编辑,因为我不能显示这些字符。见上面 –

回答

5

请打开您的编译器警告级别。这里有一个错误:

while (a=caroll.get() && !caroll.eof()) { 

这被解释为:

while (a = (caroll.get() && !caroll.eof())) { 
     ^       ^

您需要添加分配周围括号:

while ((a = caroll.get()) && !caroll.eof()) { 
    ^   ^

GCC警告说这件事。

(注:请张贴代码编译,filename是不是你的样品中声明的,你包括cstring时,你应该包括string。)

+0

这就是它。谢谢.... –