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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1447094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:00:01  来源:igfitidea点击:

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

c++visual-studiofstreamifstreamfile-management

提问by Josh Bradley

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?

我知道这是一个菜鸟问题,但我以前使用过 Python,例如,当您只想访问 .txt 文件时,您所要做的就是确保 txt 文件位于同一目录中。我在下面有以下 C++ 代码,但它没有找到我保存在桌面上的 Numbers.txt 文件。我在文件中只有一行 double 类型的数字。我想要做的就是找到文件中所有数字的平均值。该程序运行良好,但无法正确打印输出。通过仅打印 output[0] 检查以查看打印到输出中的内容后,我发现该文件并未将其内容复制到数组中。有人可以为我解决这个小问题,或者至少为我指明正确的方向,以获得一个好的教程吗?

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;
}

采纳答案by Eric J.

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.

在调试模式下运行时,Visual Studio 将工作目录设置为 YourProjectDirectory\Debug\Bin。如果您的文本文件在 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.

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

回答by J L

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. 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. Then in visual studio under source files, right click-> add existing item->(your text file)

我刚刚遇到了同样的问题,但我没有找到任何有效的答案。然后我想起了我很久以前在 OOP 中学到的东西。您需要做的是在您的桌面上获取该文本文件,然后在您的计算机文档中的 Visual Studio 项目中找到项目文件夹,然后将该文本文件放在 Visual Studio 之外的该文件夹中。然后在源文件下的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

回答by RichieHindle

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.

将您的文本文件放在某个目录中,并将工作目录设置为指向该目录。

回答by Cem Kalyoncu

Working path is project directory.

工作路径是项目目录。