<i id='AwT3b'><tr id='AwT3b'><dt id='AwT3b'><q id='AwT3b'><span id='AwT3b'><b id='AwT3b'><form id='AwT3b'><ins id='AwT3b'></ins><ul id='AwT3b'></ul><sub id='AwT3b'></sub></form><legend id='AwT3b'></legend><bdo id='AwT3b'><pre id='AwT3b'><center id='AwT3b'></center></pre></bdo></b><th id='AwT3b'></th></span></q></dt></tr></i><div id='AwT3b'><tfoot id='AwT3b'></tfoot><dl id='AwT3b'><fieldset id='AwT3b'></fieldset></dl></div>

      <bdo id='AwT3b'></bdo><ul id='AwT3b'></ul>
    1. <tfoot id='AwT3b'></tfoot>
    2. <legend id='AwT3b'><style id='AwT3b'><dir id='AwT3b'><q id='AwT3b'></q></dir></style></legend>

      <small id='AwT3b'></small><noframes id='AwT3b'>

    3. C++ 无法读取文本文件

      时间:2023-05-23

        • <tfoot id='Zcwir'></tfoot>
          <legend id='Zcwir'><style id='Zcwir'><dir id='Zcwir'><q id='Zcwir'></q></dir></style></legend>
            • <bdo id='Zcwir'></bdo><ul id='Zcwir'></ul>

              <small id='Zcwir'></small><noframes id='Zcwir'>

                  <tbody id='Zcwir'></tbody>
                <i id='Zcwir'><tr id='Zcwir'><dt id='Zcwir'><q id='Zcwir'><span id='Zcwir'><b id='Zcwir'><form id='Zcwir'><ins id='Zcwir'></ins><ul id='Zcwir'></ul><sub id='Zcwir'></sub></form><legend id='Zcwir'></legend><bdo id='Zcwir'><pre id='Zcwir'><center id='Zcwir'></center></pre></bdo></b><th id='Zcwir'></th></span></q></dt></tr></i><div id='Zcwir'><tfoot id='Zcwir'></tfoot><dl id='Zcwir'><fieldset id='Zcwir'></fieldset></dl></div>
                本文介绍了C++ 无法读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我正在尝试读取文本文件,但没有任何内容.我觉得它可能没有在我的 Visual Studio Resources 文件夹中正确链接,但是如果我双击它 - 它在 Visual Studio 中打开得很好,如果我测试它是否打开或者它是否良好,它不会遇到任何问题.该程序现在可以正常编译,但没有输出.我的命令提示符没有打印任何内容.有什么建议吗?

                I'm trying to read a text file but nothing is coming out. I feel like maybe It's not linking correctly in my Visual Studio Resources folder but if I double click it - it opens fine in visual studio and it doesn't run into any problems if I test to see if it opens or if it is good. The program compiles fine right now but there's not output. Nothing prints to my command prompt. Any suggestions?

                代码

                #include <iostream>
                #include <iomanip>
                #include <fstream>
                using namespace std;
                
                int main()
                {
                    char str[100];
                    ifstream test;
                    test.open("test.txt");
                
                    while(test.getline(str, 100, '#'))
                    {
                        cout << str << endl;
                    }
                
                    test.close();
                    return 0;
                }
                

                文本文件

                This is a test Textfile#Read more lines here#and here
                

                推荐答案

                您尝试按名称打开没有路径的文件,这意味着该文件应在您程序的当前工作目录中.

                You try to open file by name without path, this means the file shall be in current working directory of your program.

                问题出在从 VS IDE 运行程序时的当前目录.默认情况下,VS 将运行程序的当前工作目录设置为项目目录 $(ProjectDir).但是您的测试文件位于资源目​​录中.所以 open() 函数找不到它,getline() 立即失败.

                The problem is with current directory when you run your program from VS IDE. VS by default sets current working directory for runnning program to project directory $(ProjectDir). But your test file resides in resources directory. So open() function could not find it and getline() immediately fails.

                解决方案很简单 - 将您的测试文件复制到项目目录.或将其复制到目标目录(创建程序 .exe 文件的位置,通常是 $(ProjectDir)Debug$(ProjectDir)Release>) 并更改 VS IDE 中的工作目录设置:Project->Properties->Debugging->Working Directory,设置为 $(TargetDir).在这种情况下,它将在 IDE 和命令行/Windows 资源管理器中工作.

                Solution is simple - copy your test file to project directory. Or copy it to target directory (where your program .exe file is created, typically $(ProjectDir)Debug or $(ProjectDir)Release) and change working directory setting in VS IDE: Project->Properties->Debugging->Working Directory, set to $(TargetDir). In this case it will work both from IDE and command line/Windows Explorer.

                另一种可能的解决方案 - 在 open() 调用中设置正确的文件路径.出于测试/教育目的,您可以对其进行硬编码,但实际上这不是一种好的软件开发风格.

                Another possible solution - set correct path to file in your open() call. For testing/education purposes you could hardcode it, but actually this is not good style of software development.

                这篇关于C++ 无法读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:MSBuild:用于 VS2010 中本机 C++ 项目的 Custom.After.Mi 下一篇:Visual Studio 2010 中的 C++ 访问修饰符自动缩进慢慢

                相关文章

                最新文章

                <legend id='PIpTA'><style id='PIpTA'><dir id='PIpTA'><q id='PIpTA'></q></dir></style></legend>
              • <i id='PIpTA'><tr id='PIpTA'><dt id='PIpTA'><q id='PIpTA'><span id='PIpTA'><b id='PIpTA'><form id='PIpTA'><ins id='PIpTA'></ins><ul id='PIpTA'></ul><sub id='PIpTA'></sub></form><legend id='PIpTA'></legend><bdo id='PIpTA'><pre id='PIpTA'><center id='PIpTA'></center></pre></bdo></b><th id='PIpTA'></th></span></q></dt></tr></i><div id='PIpTA'><tfoot id='PIpTA'></tfoot><dl id='PIpTA'><fieldset id='PIpTA'></fieldset></dl></div>

                1. <small id='PIpTA'></small><noframes id='PIpTA'>

                    <tfoot id='PIpTA'></tfoot>
                    • <bdo id='PIpTA'></bdo><ul id='PIpTA'></ul>