Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法使用ifstream c+;读取输入文件+;_C++_Ifstream - Fatal编程技术网

C++ 无法使用ifstream c+;读取输入文件+;

C++ 无法使用ifstream c+;读取输入文件+;,c++,ifstream,C++,Ifstream,我有一个输入文件“abc.txt”,其中每行都包含以“,”分隔的字符。 当尝试使用ifstream逐行读取文件时,它无法读取文件,我在控制台上得到的输出是“无法打开输入文件”。 我做错了什么? 守则:- void EnterFiles(string filename, int index) { string line; vector<string> f1,f2; std::ifstream f; //prepare f to throw if fa

我有一个输入文件“abc.txt”,其中每行都包含以“,”分隔的字符。 当尝试使用ifstream逐行读取文件时,它无法读取文件,我在控制台上得到的输出是“无法打开输入文件”。 我做错了什么? 守则:-

void EnterFiles(string filename, int index)
{
    string line;
    vector<string> f1,f2;

    std::ifstream f;

    //prepare f to throw if failbit gets set

    std::ios_base::iostate exceptionMask = f.exceptions() | std::ios::failbit;
    f.exceptions(exceptionMask);
    try
    {
        f.open(filename);
    }
    catch (std::ios_base::failure& e)
    {
        std::cerr << e.what() << '\n';
    }
    if (!f)
    {
        cout << "Cannot open input file.\n";
    }
    while (getline(f,line) )
    {   
        if (index == 0)
        {
            f1.push_back(line);
            cout << line << endl;
        }
        else
        {
            f2.push_back(line);
            cout << line << endl;
        }
    }
    f.close();
}
void EnterFiles(字符串文件名,int索引)
{
弦线;
矢量f1,f2;
std::iff流;
//如果设置了failbit,则准备f抛出
std::ios_base::iostate exceptionMask=f.exceptions()| std::ios::failbit;
f、 例外(例外任务);
尝试
{
f、 打开(文件名);
}
捕获(标准::ios\U基::失败和e)
{

如果每次都满足条件,则跳过其他部分

提供完整或限定的文件路径(有时会产生问题!!), 另外,包括文件(如果不是!!)

下面是一个与您所引用的代码类似的示例, (由我检查,在Linux中就像一个符咒)


希望,这个答案可能会有所帮助。

使用文件的完整路径名。您没有提供文件路径,您的文件是否与您的程序位于同一目录中?如果没有,您必须提供正确的路径。您应该尝试打印原因:使用该路径,您需要从与文件相同的目录中运行程序。您如何运行程序am?包含exe的文件夹不一定是当前工作目录。请尝试以下操作:-