关于C++的文件操作和getline()的问题?

各位可以帮我看一下这段代码有没有错误(刚刚接触C++的文件操作,可能错误不止一点点):

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
    ifstream in("abc.txt");
    for(string str,getline(in,str);){
        //(具体操作);
    }
    return 0;
}

为什么总在getline那行代码报错呢?abc.txt文件和源代码在同级目录中。

额,没见过你这种写法呢 ,如果是伪代码的话,应该是没找到文件 ,不过具你说应该不可能,那么文件中有数据么?没数据会直接退出循环

另外附一个通常的写法

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
    ifstream in( "abc.txt" );
    string str;
    while ( getline( in, str ) )
    {
        cout << str << endl;
    }
    return 0;
}


图片说明

你的程序根本不能编译吧

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
    ifstream in("abc.txt");
    string str;
    for(;getline(in,str);){
        //(具体操作);
    }
    return 0;
}

abc.txt文件和源代码在同级目录中
这个不行,你不带路径只有文件名,如果是vc++,默认是vs启动的路径,也就是exe所在的路径。
你最好是用完整路径试试看