-
file reading problem
i have a file i am reading from, however it only prints part of the code and it appears to be a buffer problem. can anyone help?
Code:
#include <iostream>
#include <windows.h>
#include <fstream.h>
#include <conio.h>
#include <stdlib.h>
int numchoice;
void main()
{
char ch;
cout << "Welcome to the Airport Flight Database" << endl;
cout << " " << endl;
cout << "1 - Display all Flight information" << endl;
cout << "2 - Display details for Flight number________ " << endl;
cout << "3 - Find Flight(s) for a set route" << endl;
cout << "4 - " << endl;
cin >> numchoice;
ifstream file;
file.open("flight.txt",ios::nocreate);
if(!file)
{
cout<<"UNABLE TO OPEN FILE!!";
goto end;
}
while(file)
{
file.get(ch);
cout<<ch;
}
getch();
end:
file.close();
system("cls");
}
-
main() returns int, not void
don't use goto there
What do you mean by "it only prints part of the code"? Could you provide some sample output that highlights your problem?
-
well i have a text file which contains flight details, as follows
Code:
SQ0327~10:02 ~06:29 ~Manchester ~Singapore#
SQ0198~18:55 ~19:55 ~Singapore ~Penang#
SQ0191~10:35 ~11:42 ~Penang ~Singapore#
SQ0235~21:13 ~06:35 ~Singapore ~Brisbane#
NZ0136~12:47 ~17:40 ~Brisbane ~Auckland#
NZ006 ~16:15 ~09:15 ~Auckland ~Los Angeles#
UA6935~12:45 ~14:26 ~Los Angeles ~Phoenix#
UA0928~11:32 ~16:50 ~Phoenix ~Chicago#
UA4820~18:30 ~08:00 ~Chicago ~Manchester#
QF528 ~13:15 ~14:30 ~Sydney ~Brisbane#
QF623 ~12:40 ~15:02 ~Brisbane ~Melbourne#
QF5 ~17:00 ~23:27 ~Melbourne ~Bangkok#
QF5 ~00:22 ~07:00 ~Bangkok ~Frankfurt#
BA1707~11:00 ~11:50 ~Frankfurt ~Manchester#
SQ327 ~09:32 ~07:35 ~Manchester ~Singapore#
SQ235 ~21:15 ~06:30 ~Singapore ~Brisbane#
QF608 ~08:50 ~11:05 ~Brisbane ~Cairns#
QF944 ~17:50 ~21:15 ~Cairns ~Darwin#
QF492 ~17:43 ~19:35 ~Alice Springs ~Adelaide#
QF599 ~13:55 ~15:47 ~Adelaide ~Perth#
SQ226 ~16:02 ~21:19 ~Perth ~Singapore#
SQ328 ~23:22 ~07:55 ~Singapore ~Manchester#
%
ignore the symbols, they are for search purposes later
it prints to the start of the first qf5, but from 5 onwards i get nothing. i tried to ctrl+shift and select the rest using the cursor keys, and it cleared the screen and printed the rest of the list
-
Try
cout << ch << flush;
Maybe your output is being buffered when you don't want it to.
-
cheers mate, problem sorted :D
-