read from file

This is a discussion on read from file within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, i have a problem reading records from file i can write to th efile but cannot read back ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    24

    read from file

    Hi everyone,
    i have a problem reading records from file
    i can write to th efile but cannot read back
    can any one help wit this please
    Code:
    void writeAppointmenRecords( )
    {
    	int index;
        //create and open an output text file
    
            fstream outfile("C:\\Documents and Settings\\zafer\\Desktop\\AppointmenRecords.txt", ios::app);
    // Check if file is opened
                if(!outfile)          //return true if file is not opened
    	  {  cout<<"\nFailed to open file!!!!\n";
             cout<<"\nPress any key to proceed ";
    		 cin.get();                 
    	  }
    		
    	for (index=0; index<currentSize; index++)
    	{
            outfile<<AppointmenList[index].name;
    		outfile<<setw(15)<<AppointmenList[index].description;
    		outfile<<setw(15)<<AppointmenList[index].appdate.day;
    		outfile<<"/"<<AppointmenList[index].appdate.mounth;
    		outfile<<"/"<<AppointmenList[index].appdate.year;
    		outfile<<setw(15)<<AppointmenList[index].time<<endl;
    		
    	}  
         //write values to file in an 15 characters field.
    	
        outfile.close( );    // close file
        cin.get();
    
    }
    
    void readAppointmenRecords( )
    
    {  
    	currentSize=0;
    	//create a stream and open the file 'AppointmenRecords.txt' for input
    
    	 fstream infile("C:\\Documents and Settings\\zafer\\Desktop\\AppointmenRecords.txt", ios::in);
    		// check if file is opened
    		  if(!infile)          //return true if file is not opened
    	  {  cout<<"\nFailed to open file!!!!\n";
    	                            //indicate program failed
    	  cin.get();
    	  }
    	while (!infile.eof( ))    //eof( ) End Of File function. Returns false if end file reached
           {
    		getline(infile, AppointmenList[currentSize].name);
    		getline(infile, AppointmenList[currentSize].description);
    		infile.get();
    		infile>>AppointmenList[currentSize].appdate.day;
    		infile>>AppointmenList[currentSize].appdate.mounth;
    		infile>>AppointmenList[currentSize].appdate.year;
    		getline(infile, AppointmenList[currentSize].time);
    		currentSize+=1;
            }
        infile.close( ); // close file
    	currentSize = currentSize -1;
    	cin.get();
        
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,450
    do not use eof() to control loop - read FAQ

    Are you sure your input is using several lines? I do not see you outputting endl character, have you checked the file structure?

    check the return value of each read operation - see when it is failed

    could you debug your code?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    Tnaks for replaying vart
    first im only beginner so i dont know to much yet.

    this is my entry
    How many Appointmen you wish to add?

    Enter Person name: zafer
    Enter Appointmen Descriptions: luch
    Enter Appointmen date: 15 04 2009
    Enter Appointmen time: 13:30


    Enter Person name: zafer
    Enter Appointmen Descriptions: dinner
    Enter Appointmen date: 15 04 2009
    Enter Appointmen time: 21:00
    and this is in the file

    zafer luch 15/4/2009 13:30
    zafer dinner 15/4/2009 21:00
    i remove the eof() but still same

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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