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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/798798/
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 17:19:56  来源:igfitidea点击:

ifstream::open not working in Visual Studio debug mode

c++visual-studioifstream

提问by Dave Swersky

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.

我已经解决了关于 SO 的所有 ifstream 问题,但我仍然无法阅读简单的文本文件。我正在使用 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.txtfile is in the correct "working directory" by checking the value of argv[0]. The Open method just won't work.

我已通过检查 .txt 的值确认input.txt文件位于正确的“工作目录”中argv[0]。Open 方法是行不通的。

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

我在调试时也遇到了问题 - 我应该无法设置手表infile.good()还是infile.is_open()?我不断得到

Error: member function not present.

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

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

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

更新:该文件不在当前工作目录中。这是项目文件所在的目录。将它移到那里,它在 VS.NET 中调试时有效。

回答by Bill the Lizard

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

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

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

The openmodeparameter is a bitmask. ios::ateis used to open the file for appending, and ios::inis used to open the file for reading input.

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

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++.

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

回答by Dustin Taylor

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.

有时,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);

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

希望这有助于其他人快速解决。我自己花了一段时间才找到这个设置。

回答by 3DH

I've found two problems in your code:

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

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

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

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

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

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

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

ciao, Chris

乔,克里斯

回答by Tom

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

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

||是逻辑或运算符,而不是按位运算符(如 Bill The Lizzard 所说)。

so i guess you are doing the equivalent to:

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

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

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

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

回答by dirkgently

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."

我在调试时也遇到了问题——我应该无法在“infile.good()”或“infile.is_open()”上设置监视吗?我不断收到“错误:成员函数不存在”。

and the proper includes:

适当的包括:

#include <fstream> 

etc.

等等。