尝试在文本文件中添加结构元素而不覆盖它

try to add structur element in a text file without overwrite it

void addToTextfile()
{
    Students stud[20];
    Students stdt;
    ifstream myFile;

    myFile.open("student.txt", fstream::app);

    if (myFile.is_open())
    {
        cout << "\t\t\tStudent KNumber     =>  ";
        cin >> stdt.KNumber;
        cout << "\t\t\tStudent Name        =>  ";
        cin >> stdt.StudentName;

        myFile << stdt.KNumber << stdt.StudentName << endl;

    }
    myFile.close();
}

获取错误 =>

Error 1 error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'std::ifstream' (or there is no acceptable conversion)

更改您的文件变量以使用 fstreamifstream 用于 input。 ofstream 用于 o 输出。 fstream 用于输入和输出。

operator<<用于输出。您不能输出到输入流(不要进入出口门)。