1

I am trying to read from two text files. One at a time, but I keep getting the couldn't open file message. I've tried to make sure, they are in the same directory, however it still doesn't work. Could anyone tell me where is a mistake?

This is a constructor of a class which reads from 2 text files and fills up two different arrays.

here is the code:

#include <iostream>
#include <fstream>
#include "student.h"
#include "staff.h"
#include "studentstaff.h"

using namespace std;

studentstaff::studentstaff()
{
    p = new Student[5];
    p1 = new Staff[5];

    ifstream file;
    file.open("Student.txt");

    if (!file)
    {
        cout << "File couldn't open." << endl;
    }
    else
    {
        int studentID;
        string firstName;
        string lastName;
        string DOB;
        char program;
        int credit;
        int startYear;
        double GPA;

        file >> studentID >> firstName >> lastName >> DOB >> program >> credit >> startYear >> GPA;

        while (file)
        {
            for (int i = 0; i < 5; i++)
            {
                file >> studentID >> firstName >> lastName >> DOB >> program >> credit >> startYear
                    >> GPA;
                p[i].setStudent(
                    studentID, firstName, lastName, DOB, program, credit, startYear, GPA);
            }
        }
        file.close();
    }

    ifstream stafffile;
    stafffile.open("staff.txt");

    if (!stafffile)
    {
        cout << "File couldn't open." << endl;
    }
    else
    {
        string employeefstname;
        string employeelstname;
        int ID;
        string phonenum;
        int datehired;
        char Code;
        double salary;
        stafffile >> employeefstname >> employeelstname >> ID >> phonenum >> datehired >> Code
            >> salary;

        while (stafffile)
        {
            for (int i = 0; i < 5; i++)
            {
                stafffile >> employeefstname >> employeelstname >> ID >> phonenum >> datehired
                    >> Code >> salary;
                p1[i].setfirstname(employeefstname);
                p1[i].setlastname(employeelstname);
                p1[i].setID(ID);
                p1[i].setphonenum(phonenum);
                p1[i].setdatehired(datehired);
                p1[i].setbonuscode(Code);
                p1[i].setsalary(salary);
            }
        }
        stafffile.close();
    }
}
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • 2
    Please post a [mcve]. This includes the folder hirearchy you use. You can try to use `std::ofstream file("test.txt");` and see where in the filesystem it ends up. Then you can put the other files at that location. Note that some IDEs use different working folders when you debug your code or for release versions. – Quimby Dec 18 '20 at 17:43
  • Replace `cout << "File couldn't open." << endl;` with `std::perror("Student.txt");` to get better error information on standard error stream. Provide this message here. I'm betting your executable is run from different location then directory of `"Student.txt"` file. You can print `std::cout << argv[0] << '\n';` from main function. – Marek R Dec 18 '20 at 18:06
  • Student.txt: No such file or directory this is the message i get from perror – Magy Gerges Dec 18 '20 at 18:30
  • Does this answer your question? [Fast textfile reading in c++](https://stackoverflow.com/questions/17925051/fast-textfile-reading-in-c) – StarShine Dec 18 '20 at 20:57
  • @MagyGerges so it is obvious your problem is file location (current path and path of your file are different) not code problem. One of crappy solutions is to provide full absolute path to the file. – Marek R Dec 19 '20 at 08:58

1 Answers1

0

You should write like this:

int i = 0;
while(file >> studentID >> firstName >> lastName >> DOB >> program >> credit >> startYear >> GPA){

    p[i].setStudent(studentID, firstName, lastName, DOB, program, credit, startYear, GPA);
    i++;
}

Or, if you have exactly five records on the input file, then:

for(int i = 0; i<5; i++){

    file >> studentID >> firstName >> lastName >> DOB >> program >> credit >> startYear >> GPA;
    p[i].setStudent(studentID, firstName, lastName, DOB, program, credit, startYear, GPA);
}

Also check the file name at file.open("Student.txt");, if you have made any mistake or not.

Astik Roy
  • 38
  • 6