【问题标题】:Why doesn't the text file get read?为什么不读取文本文件?
【发布时间】:2022-08-14 15:16:42
【问题描述】:

继续我之前的帖子^^

myfile 未打开: void readStudentProfile(fstream&myFile, string studentname, string studentclass, string email, string cca, int adminNum) { ... }

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void enterStudentProfile(fstream&myFile, string studentname, string studentclass, string email, string cca, int adminNum);
void readStudentProfile(fstream&myFile, string studentname, string studentclass, string email, string cca, int adminNum);

int main()
{
    fstream myFile;
    string studentname, studentclass, email, cca;
    char choiceProfile;
    int adminNum = 0;
    enterStudentProfile(myFile, studentname, studentclass, email, cca, adminNum);

    myFile.open(\"Student Profile.txt\");

    cout << \"Would you like to see your Student Profile (Y/N)? \";
    cin >> choiceProfile;

    if (choiceProfile == \'Y\' || choiceProfile == \'y\') readStudentProfile(myFile, studentname, studentclass, email, cca, adminNum);
    else if (choiceProfile == \'N\' || choiceProfile == \'y\') cout << \"Alright!\";
    else cout << \"Invalid response.\";



    system(\"pause\");
    return 0;
}

void enterStudentProfile(fstream&myFile, string studentname, string studentclass, string email, string cca, int adminNum)
{

    cout << \"Enter your full name (without leaving blanks): \";
    cin >> studentname;

    cout << \"Enter your admin number: \";
    cin >> adminNum;

    cout << \"Enter your student class (without leaving blanks): \";
    cin >> studentclass;

    cout << \"Enter your iChat email (without leaving blanks): \";
    cin >> email;

    cout << \"Enter your Co-Curricular Activity (without leaving blanks): \";
    cin >> cca;

    cout << endl;

    myFile.open(\"Student Profile.txt\", ios::out);

    if (myFile.is_open())
    {
        myFile << studentname << \" \" << adminNum << \" \" << studentclass << \" \" << email << \" \" << cca;

        myFile.close();
    }
    else cout << \"Failure.\";
}

void readStudentProfile(fstream&myFile, string studentname, string studentclass, string email, string cca, int adminNum)
{
    cout << \"Loading...\\n\";

    myFile.open(\"Student Profile.txt\");

    cout << \"Loading...\\n\";

    if (!myFile)
        cout << \"Error\\n\";
    else
    {
        cout << \"Your full name (without leaving blanks is \" << studentname << \".\" << endl << \"Your admin number is \" << adminNum << \".\" << endl << \"Your student class is \" << studentclass << \".\" << endl
            << \"Your iChat email is \" << email << \".\" << endl << \"You Co-Curricular Activity is \" << cca << \".\\n\";
    }

    myFile.close();

}

输出:

您想查看您的学生资料(Y/N)吗?是

正在加载... 正在加载...错误按任意键继续 。 . .

发送帮助谢谢!

另外,如果还有其他方法可以改进我的程序,请给我建议。

    标签: c++ text-files fstream


    【解决方案1】:

    考虑命令其中的声明

    enterStudentProfile(myFile, studentname, studentclass, email, cca, adminNum);
    
    myFile.open("Student Profile.txt");
    

    你应该打开文件你试着从中读取,像这样

    myFile.open("Student Profile.txt");
    enterStudentProfile(myFile, studentname, studentclass, email, cca, adminNum);
    

    【讨论】:

      猜你喜欢
      • 2018-07-21
      • 2023-03-06
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 2020-09-09
      相关资源
      最近更新 更多