Read File

No error, but it does not read file

My class include string acno2, name_st, date2 and address_st; and I want to add new student:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  account ac;
	fstream outFile;
	outFile.open("Student.txt", ios::app | ios::out | ios::in);
	string dismiss;
	string acno;
	if (!outFile)
	{
		cout << "File could not be open !! Press any Key...";
	}
    else
    {
        ac.student_creac();
        while (!outFile.eof())
        {
            getline(outFile,acno,',');
            if (getline(outFile,dismiss,'\n'))
                if (ac.acno2==acno)
                {
                cout << "\tAccount existed, please enter another account." << endl;
                outFile.close();
                add_st();
                }
        }
    }
    outFile << ac.acno2 << ',' << ac.name_st << ',' << ac.date2 << ',' << '"' << ac.address_st << '"' << '\n';
    cout << "added" << endl;
	outFile.close();


Here my text:
1
2
3
4
5
6
7
1712345,NONG THI HANH,1987-09-07,"Huyen Luc Nam, Bac Giang"
1712346,TRAN XUAN SON,1985-10-22,"Huyen Pho Yen, Thai Nguyen"
1712347,PHAM THI TRANG,1987-03-22,"Huyen Dong Hy, Thai Nguyen"
1712348,TRAN THI NGOC ANH,1985-06-20,"Huyen Phu Binh, Thai Nguyen"
1712349,MA THI THU UYEN,1991-11-22,"Huyen Tran Yen, Yen Bai"
1712350,DAO THI DUYEN,1988-05-27,"Huyen Phu Binh, Thai Nguyen"
1712351,NONG THI SEN,1986-10-26,"Huyen Pho Yen, Thai Nguyen"


Regard.
Where is your text file? Where is your executable file? Where is your project file, if any?

If you're using an IDE, sometimes the default working directory is where the project file is, and not in the /bin folder. Try moving your file around.

Also, if on windows, you can try
system("cd"); to see where your program is executing from.

On Linux/Mac, do system("pwd");
my text file and executable file inside project file (where program is executing), i use this code to show (and even modify), and the same file it work properly but to add new data from the above code it is not work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
account Lst;
    string dismiss;
	ifstream inFile2;
	inFile2.open("Student.txt", ios::in);
	if(!inFile2)
	{
		cout << "File could not be open !! Press any Key...";
	}
    while (inFile2)
    {
        getline(inFile2,Lst.acno2,',');
        getline(inFile2,Lst.name_st,',');
        getline(inFile2,Lst.date2,',');
        getline(inFile2,dismiss,'"');
        getline(inFile2,Lst.address_st,'"');
        if (!inFile2)
        {
            break;
        }
        Lst.report2();
    }
    inFile2.close();


Thanks
Last edited on
So it does open the file correctly, but fails at some point when reading.

I'm not sure then, try running your code through a debugger line-by-line so that you can tell where it starts to fail.
Topic archived. No new replies allowed.