简体   繁体   English

Visual Studio在进行文件管理操作时,在哪里搜索txt文件?

[英]Where does Visual Studio search for txt files when conducting file management operations?

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.我知道这是一个菜鸟问题,但我以前使用过 Python,例如,当您只想访问 .txt 文件时,您所要做的就是确保 txt 文件位于同一目录中。 I have the following C++ code below but it's not finding the Numbers.txt file that I have saved on my desktop.我在下面有以下 C++ 代码,但它没有找到我保存在桌面上的 Numbers.txt 文件。 All I have in the file is one line of numbers of type double.我在文件中只有一行 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.通过仅打印 output[0] 检查以查看打印到输出中的内容后,我发现该文件并未将其内容复制到数组中。 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 sets the working directory to YourProjectDirectory\\Debug\\Bin when running in debug mode.在调试模式下运行时,Visual Studio 将工作目录设置为 YourProjectDirectory\\Debug\\Bin。 If your text file is in YourProjectDirectory, you need to account for that difference.如果您的文本文件在 YourProjectDirectory 中,则需要考虑该差异。

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.最简单的方法是将您的文本文件包含在项目中,并将它们的构建操作(在“属性”窗口中)设置为“内容”。

I just had this same problem, and I didn't find any of those answers to work.我刚刚遇到了同样的问题,但我没有找到任何有效的答案。 Then I remembered what I learned a long time ago in OOP.然后我想起了我很久以前在 OOP 中学到的东西。 What you have to do is take that text file on your desktop, and find the project folder in your visual studio projects within your computers documents, and put the text file in that folder outside of visual studio.您需要做的是在桌面上获取该文本文件,然后在计算机文档中的 Visual Studio 项目中找到项目文件夹,然后将该文本文件放在 Visual Studio 之外的该文件夹中。 Then in visual studio under source files, right click-> add existing item->(your text file)然后在源文件下的visual studio中,右键单击->添加现有项目->(您的文本文件)

:) :)

btw I bumped this thread because this thread said it was a good idea, and I wanted it updated for the sake of people googling the same question.顺便说一句,我撞到了这个帖子,因为这个帖子说这是一个好主意,我希望它更新,以便人们在谷歌上搜索相同的问题。 https://meta.stackexchange.com/questions/125965/is-bumping-old-questions-allowed https://meta.stackexchange.com/questions/125965/is-bumping-old-questions-allowed

If you're talking about running the code within the Visual Studio debugger via F5 or Debug / Start Debugging, you can set the working directory of your program via Project / <Project name> Properties / Configuration / Debugging / Working directory.如果您正在讨论通过 F5 或 Debug / Start Debugging 在 Visual Studio 调试器中运行代码,您可以通过 Project / <Project name> Properties / Configuration / Debugging / Working directory 设置程序的工作目录。

Put your text file in a directory somewhere, and set Working directory to point to that directory.将您的文本文件放在某个目录中,并将工作目录设置为指向该目录。

工作路径是项目目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM