ifstream::open 在 Visual Studio 调试模式下不起作用

发布于 2024-07-19 07:13:42 字数 1082 浏览 3 评论 0原文

我已经在这里讨论了 ifstream 问题,但我仍然无法阅读简单的文本文件。 我正在使用 Visual Studio 2008。

这是我的代码:

// CPPFileIO.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <conio.h>
#include <iostream>
#include <string>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{

    ifstream infile;
    infile.open("input.txt", ifstream::in);

    if (infile.is_open())
    {
        while (infile.good())
            cout << (char) infile.get();
    }
    else
    {
        cout << "Unable to open file.";
    }
    infile.close();
    _getch();
    return 0;
}

通过检查 argv[0]< 的值,我已确认 input.txt 文件位于正确的“工作目录”中/代码>。 Open 方法根本不起作用。

我在调试时也遇到了问题 - 我是否无法在 infile.good()infile.is_open() 上设置监视? 我不断收到

Error: member function not present.

编辑:使用 .CPP 文件中的完整代码更新的代码列表。

更新:该文件不在当前工作目录中。 这是项目文件所在的目录。 将其移至此处,在 VS.NET 中调试时它可以工作。

I've been all over the ifstream questions here on SO and I'm still having trouble reading a simple text file. I'm working with Visual Studio 2008.

Here's my code:

// CPPFileIO.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <conio.h>
#include <iostream>
#include <string>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{

    ifstream infile;
    infile.open("input.txt", ifstream::in);

    if (infile.is_open())
    {
        while (infile.good())
            cout << (char) infile.get();
    }
    else
    {
        cout << "Unable to open file.";
    }
    infile.close();
    _getch();
    return 0;
}

I have confirmed that the input.txt file is in the correct "working directory" by checking the value of argv[0]. The Open method just won't work.

I'm also having trouble debugging- should I not be able to set a watch on infile.good() or infile.is_open()? I keep getting

Error: member function not present.

EDIT: Updated code listing with full code from .CPP file.

UPDATE: The file was NOT in the Current Working Directory. This is the directory where the project file is located. Moved it there and it works when debugging in VS.NET.

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

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

发布评论

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

评论(6

韶华倾负 2024-07-26 07:13:42

指定打开模式时尝试使用按位或运算符。

infile.open ("input.txt", ios::ate | ios::in);

openmode 参数是一个位掩码。 ios::ate 用于打开文件进行追加,ios::in 用于打开文件进行读取输入。

如果您只想读取该文件,您可能可以使用:

infile.open ("input.txt", ios::in);

ifstream 的默认打开模式是 ios::in,因此您现在可以完全摆脱它。 以下代码适用于我使用 g++ 的情况。

#include <iostream>
#include <fstream>
#include <cstdio>

using namespace std;

int main(int argc, char** argv) {
    ifstream infile;
    infile.open ("input.txt");

    if (infile)
    {
        while (infile.good())
            cout << (char) infile.get();
    }
    else
    {
        cout << "Unable to open file.";
    }
    infile.close();
    getchar();
    return 0;
}

Try using the bitwise OR operator when specifying the open mode.

infile.open ("input.txt", ios::ate | ios::in);

The openmode parameter is a bitmask. ios::ate is used to open the file for appending, and ios::in is used to open the file for reading input.

If you just want to read the file, you can probably just use:

infile.open ("input.txt", ios::in);

The default open mode for an ifstream is ios::in, so you can get rid of that altogether now. The following code is working for me using g++.

#include <iostream>
#include <fstream>
#include <cstdio>

using namespace std;

int main(int argc, char** argv) {
    ifstream infile;
    infile.open ("input.txt");

    if (infile)
    {
        while (infile.good())
            cout << (char) infile.get();
    }
    else
    {
        cout << "Unable to open file.";
    }
    infile.close();
    getchar();
    return 0;
}
谷夏 2024-07-26 07:13:42

有时,Visual Studio 会将 exe 文件与源代码分开。 默认情况下,VS 可能只查找从您的 exe 文件开始的文件。 此过程是从与源代码相同的目录获取输入 txt 文件的简单步骤。 如果您不想修复 IDE 设置。

using namespace std;

ifstream infile;

string path = __FILE__; //gets source code path, include file name
path = path.substr(0,1+path.find_last_of('\\')); //removes file name
path+= "input.txt"; //adds input file to path

infile.open(path);

希望这可以帮助其他人快速解决问题。 我自己花了一段时间才找到这个设置。

Sometimes Visual Studio puts your exe file away from your source code. By default VS may only look for the file starting from your exe file. This process is a simple step for getting the input txt file from the same directory as your source code. Should you not want to fix your IDE setup.

using namespace std;

ifstream infile;

string path = __FILE__; //gets source code path, include file name
path = path.substr(0,1+path.find_last_of('\\')); //removes file name
path+= "input.txt"; //adds input file to path

infile.open(path);

Hopefully this helps other people for a quick solution. It took me a while to find this setup myself.

过潦 2024-07-26 07:13:42

我在您的代码中发现了两个问题:

a)“ios::ate || ios::in”=> 中的语法错误 应该是“ios::ate | ios::in”

b) “ios::ate”将光标设置到文件末尾 - 所以当你开始阅读时你什么也得不到

所以只需删除“ios::ate”就可以了很好:)

再见,
克里斯

I've found two problems in your code:

a) syntax error in "ios::ate || ios::in" => should be "ios::ate | ios::in"

b) "ios::ate" sets the cursor to the end of file - so you get nothing when you start reading

So just remove "ios::ate" and you are fine :)

ciao,
Chris

樱花坊 2024-07-26 07:13:42
infile.open ("input.txt", ios::ate || ios::in);

|| 是逻辑或运算符,而不是按位运算符(正如蜥蜴比尔所说)。

所以我猜你正在做相当于:(

infile.open ("input.txt", true);

假设 ios::ate 或 ios::in 都不为 0)

infile.open ("input.txt", ios::ate || ios::in);

|| is the logical or operator, not the bitwise operator (as Bill The Lizzard said).

so i guess you are doing the equivalent to:

infile.open ("input.txt", true);

(assuming neither ios::ate or ios::in are 0)

番薯 2024-07-26 07:13:42

尝试使用:

ifstream fStm("input.txt", ios::ate | ios::in);

我在调试时也遇到了麻烦 - 我是否无法在“infile.good()”或“infile.is_open()”上设置监视? 我不断收到“错误:成员函数不存在。”

正确的包括:

#include <fstream> 

等等。

Try using:

ifstream fStm("input.txt", ios::ate | ios::in);

I'm also having trouble debugging- should I not be able to set a watch on "infile.good()" or "infile.is_open()"? I keep getting "Error: member function not present."

and the proper includes:

#include <fstream> 

etc.

鹿港巷口少年归 2024-07-26 07:13:42

如果您使用默认的 Vs code 设置,请将您想要读取的文本文件放在与可执行文件相同的文件夹中,我知道它不太漂亮,但它可以工作

If you use the default Vs code setup, place the text file that you want to read from in the same folder as your executable, I know it is not pretty but yeah it works

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