调试时从文件中读入
我的项目的 Debug 文件夹中有 SOME_TEXT.TXT。当我手动转到“调试”文件夹并运行应用程序时,我得到了预期的输出,一切都很顺利。但是,当我在 Visual Studio 中调试应用程序时,txtFile.is_open() 失败并且我的输出不起作用。我查看了其他目录,没有看到其他可执行文件。我什至尝试过随意传播 SOME_TEXT.TXT,以防我错过了某个目录。有什么想法吗?
我有以下代码:
string path = "SOME_TEXT.TXT";
ifstream txtFile;
txtFile.open(path, ifstream::in);
char line[200];
if(txtFile.is_open())
{
int lineNumber = 1;
while(!txtFile.eof())
{
txtFile.getline(line, 200);
Line * ln = new Line(line, path, lineNumber);
lineNumber++;
myList.addLine(ln);
}
}
myList.printAll();
I have SOME_TEXT.TXT in the Debug folder for my project. When I manually go to the Debug folder and run the application, I get the expected output and all is glorious. However, when I Debug the app within Visual Studio txtFile.is_open() fails and my output doesn't work. I've looked around in the other directories and I see no other executable. I've even tried liberally spreading SOME_TEXT.TXT around in case I missed a directory. Any ideas?
I have the following code:
string path = "SOME_TEXT.TXT";
ifstream txtFile;
txtFile.open(path, ifstream::in);
char line[200];
if(txtFile.is_open())
{
int lineNumber = 1;
while(!txtFile.eof())
{
txtFile.getline(line, 200);
Line * ln = new Line(line, path, lineNumber);
lineNumber++;
myList.addLine(ln);
}
}
myList.printAll();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在项目上下文菜单的属性中控制程序运行的工作目录。然后在“调试”选项卡中。
该目录默认为可执行文件的输出目录。请注意,发布和调试的输出目录是不同的。它们通常称为“调试”和“发布”。
尝试将文本文件粘贴到“调试”文件夹中。
You can control the working directory your program will run at in properties in the context menu of your project. Then in the "Debug" tab.
This directory defaults to the output directory of the executable. Note that the output directory for release and debug are different. They are typically called "Debug" and "Release".
Try to stick your text file in the Debug folder.