【发布时间】:2022-12-29 07:38:20
【问题描述】:
我只是将要打印的行放在 for 循环内的文件中,以便将其打印五次,但没有打印任何内容。我的代码如下:
int main() {
ofstream (fileAccess);
fileAccess.open ("fileName.txt", ofstream::app);
for (int i; i < 5; i++) {
fileAccess << "Hello World!";
}
fileAccess.close();
}
请帮忙
【问题讨论】:
-
打字错误?
for (int i; ...i未初始化。 -
fileName.txt存在吗? -
旁注:
fileAccess.close();行是不必要的,因为调用析构函数时文件将自动关闭。 -
你决定不run this code in your debugger了吗?我想你会在几秒钟内解决这个问题。
-
您是否尝试过在调试器中逐行运行您的代码,同时监视所有变量的值,以确定您的程序在哪一行停止按预期运行?如果你没有尝试过这个,那么你可能想读这个:What is a debugger and how can it help me diagnose problems?你可能还想读这个:How to debug small programs?