Visual Studio在执行文件管理操作时搜索txt文件的位置? [英] Where does Visual Studio search for txt files when conducting file management operations?

查看:212
本文介绍了Visual Studio在执行文件管理操作时搜索txt文件的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个noob的问题,但我曾经与Python工作之前,当你想简单地访问一个.txt文件,例如,你所要做的是确保txt文件在同一个目录。我有下面的C ++代码,但它不是找到我保存在我的桌面上的Numbers.txt文件。我在文件中所有的一行数字类型double。所有我想做的是找到文件中所有数字的平均值。程序运行正常,但它不能正确打印输出。在通过打印输出[0]检查看到什么是打印输出之后,我发现文件没有将其内容复制到数组中。有人能为我清楚这个小问题,或至少指出我的正确方向一个很好的教程吗?

I know this is a noob question, but I've worked with Python before and when you wanted to simply access a .txt file for example, all you had to do was make sure the txt file was in the same directory. I have the following C++ code below but it's not finding the Numbers.txt file that I have saved on my desktop. All I have in the file is one line of numbers of type double. All I want to do is to find the average of all of the numbers in the file. The program runs fine, but it doesn't print the output correctly. After checking to see what is printing into output by just printing output[0], I've discovered that the file is not copying it's contents into the array. Could someone clear this little problem up for me or at least point me in the right direction to a good tutorial?

int main() {
    cout << "Getting File Information..." << endl;
    ifstream file;
    char output[100];
    //int x;

    file.open("Numbers.txt", ios::in);    // open file

    cout << "Opened File Successfully ****************" << endl;
    file >> output;              // empty file contents into output
    cout << output;              // print out contents of file
    cout << "Should have printed out results by now" << endl;
    //file >> x;

    file.close();

    return 0;
}


推荐答案

Visual Studio设置工作目录到YourProjectDirectory\Debug\Bin时在调试模式下运行。如果您的文本文件在YourProjectDirectory中,您需要考虑到这一差异。

Visual Studio sets the working directory to YourProjectDirectory\Debug\Bin when running in debug mode. If your text file is in YourProjectDirectory, you need to account for that difference.

最简单的方法是将您的文本文件包含在项目中,操作(在属性窗口中)到内容。

The easiest way to do that is to include your text files in the project and set their build action (in the Properties window) to Content.

这篇关于Visual Studio在执行文件管理操作时搜索txt文件的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆