【发布时间】:2016-03-01 23:51:45
【问题描述】:
我只是安装了 CLion 并尝试使用它。但是我在读取文件时遇到了一些问题,控制台没有按预期显示,这是我的代码。
#include <iostream>
#include <fstream>
using namespace std;
void readFile(const char *fileName);
int main() {
readFile("test.txt");
}
void readFile(const char *fileName) {
ifstream file(fileName);
string s1;
file >> s1;
cout << "s1 = " << s1;
}
这是我的文件:test.txt
hello
这是结果:
C:\Users\hongn\.CLion12\system\cmake\generated\85e35c99\85e35c99\Debug\Test.exe
s1 =
Process finished with exit code 0
当我尝试时:
int a;
cout << "a = ";
cin >> a;
cout << "a is: " << a;
这是结果:
C:\Users\hongn\.CLion12\system\cmake\generated\85e35c99\85e35c99\Debug\Test.exe
a =3
a = 3
a is: 3
Process finished with exit code 0
输入 3 后,出现“a = 3”行。
这不是预期的结果,如此多余,为什么“a = 3”行出现了2次?我怎样才能解决这个问题?
感谢您的帮助!
【问题讨论】:
-
可能
test.txt不是您的应用程序正在寻找的位置。 -
与main.cpp在同一个文件夹
-
我想,可能是我的 mingw 不适合 Clion。我曾尝试安装多个 mingw,但它们都得到相同的结果,我无法读取文件 test.txt,而它与 CodeBlock 完美运行。
-
1) 在运行/调试配置中设置“工作目录”,否则您不知道要在哪个路径中查找
"test.txt"。2) 我遇到了 CLion 乱码之前我的程序的输出,通常运行几次就可以清除它。它似乎也去掉了空白行,所以在指责你的代码之前从终端运行它以确保:)