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

发布于 2024-08-05 08:25:19 字数 818 浏览 2 评论 0原文

我知道这是一个菜鸟问题,但我之前使用过 Python,例如,当您只想访问 .txt 文件时,您所要做的就是确保 txt 文件位于同一目录中。我有以下 C++ 代码,但它找不到我保存在桌面上的 Numbers.txt 文件。我在文件中只有一行双精度类型的数字。我想做的就是找到文件中所有数字的平均值。程序运行良好,但无法正确打印输出。通过仅打印输出 [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;
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野却迷人 2024-08-12 08:25:19

在调试模式下运行时,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.

你没皮卡萌 2024-08-12 08:25:19

如果您正在谈论通过 F5 或 Debug / Start Debugging 在 Visual Studio 调试器中运行代码,您可以通过 Project /设置程序的工作目录属性/配置/调试/工作目录。

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

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.

Put your text file in a directory somewhere, and set Working directory to point to that directory.

苏别ゝ 2024-08-12 08:25:19

我刚刚遇到了同样的问题,但我没有找到任何有效的答案。然后我想起了很久以前在OOP中学到的东西。
您需要做的就是将该文本文件放在桌面上,然后在计算机文档中的 Visual Studio 项目中找到项目文件夹,然后将文本文件放入 Visual Studio 外部的该文件夹中。然后在Visual Studio中的源文件下,右键->添加现有项目->(您的文本文件)

:)

顺便说一句,我撞到了这个帖子,因为这个帖子说这是一个好主意,我希望它能够更新,以便人们在谷歌上搜索同样的问题。
https://meta.stackexchange.com/questions/125965/is-bumping-old-允许提问

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)

:)

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

紅太極 2024-08-12 08:25:19

工作路径是项目目录。

Working path is project directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文