file reading problem

This is a discussion on file reading problem within the C++ Programming forums, part of the General Programming Boards category; i have a file i am reading from, however it only prints part of the code and it appears to ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    84

    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");
    
    
    
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    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?

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    84
    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

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Try

    cout << ch << flush;

    Maybe your output is being buffered when you don't want it to.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    84
    cheers mate, problem sorted

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    You're welcome

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 06:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21