problem with read from file

This is a discussion on problem with read from file within the C++ Programming forums, part of the General Programming Boards category; hi everyone i am having problem with read from file, i can write to the filr but can not read ...

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

    problem with read from file

    hi everyone
    i am having problem with read from file, i can write to the filr but can not read from file
    can anyone have a look at my codes or give me some examples so i can have look and try to see mt mistakes thanks
    Code:
    void writeAppointmenRecords( )
    {
    	int index;
        //create and open an output text file
    
            ofstream 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
    
    	 ifstream 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( ) 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
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    Code:
    while (!infile)
    Isn't that just returning false right away when you have input?
    Try to remove the negation and try again.

    Also, I haven't really looked at the rest of the code.. If you're having errors, please post them.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    Thanks for replaying Litz
    actualy it was
    while (!infile.eof())
    like this but i changed when i was tring different thinks.
    im now getting any errors or warnings.
    so what can be wrong with my codes????

    here is my all program(trying to make) code if you want you can run it and see yourself thanks.
    Code:
    #include<iostream>
    #include<iomanip>
    #include<cstdlib>
    #include<string>
    #include<fstream>
      using namespace std;
    //declare structure types 
      struct date{
    	  int day, mounth ,year;
      };
      struct AppointmenRecord{ 
    		string name;
    		string description;
    		date appdate;
    		string time;
      };
    
    const int listSize = 30;    //number of records in array
    int currentSize = 0;
    //declare a list of records.
    	AppointmenRecord AppointmenList[listSize];     //global array of records
    
    // Functions prototypes.  
    void addAppointmenRecords( );
    void deleteAppointmenRecords( );
    void editAppointmenRecords( );
    void displayRecords( );
    void readAppointmenRecords( );
    void writeAppointmenRecords( );
    void displayMenu(int &option);
    
    
     
    void main( )
    {
        int option; 
        bool endOfSession = false;
    //get user action choice
       while (!endOfSession)
       {	
           displayMenu(option);
    
    	   switch (option)
           {   
             case 1: addAppointmenRecords( );
                    	 break;
    	   //case 2: deleteAppointmenRecords( );
                    	 break;
    	   //case 3: editAppointmenRecords( );
                    	 break;
             case 4: displayRecords( );
                    	 break;
    		 case 5: writeAppointmenRecords( );
                    	 break;
    		 case 6: readAppointmenRecords( );
                    	 break;
             case 7: system("cls");
                     	 cout<<"\nEND OF SESSION\n\n";
                    	 endOfSession = true;
                    	 break;
             default: cout<<endl<<"Wrong option number!! Try again\n";
    			 cin.get();
           }
       }
    }
    
    void displayMenu(int &option)
    { // display menu options and choose one 
    
         system("cls");        //clear screen
                     
        cout<<endl<<endl;
        cout<<"\t    * APPOINTMENTS DATA MENU *"<<endl<<endl;
        cout<<"\t  1. Add An Appointmen Record(s)"<<endl;
        cout<<"\t  2. Delete An Appointmen Record(s)"<<endl;
        cout<<"\t  3. Edit An Appointmen Record(s)"<<endl;
        cout<<"\t  4. Display Appointmens Records"<<endl;
        cout<<"\t  5. Write An Appointmen Record To The File"<<endl;
        cout<<"\t  6. Read An Appointmen Record From The File"<<endl;
        cout<<"\t  7. End Session"<<endl;
        cout<<endl<<setw(28)<<"Enter option number: ";
    
        cin>>option;
        cin.get();
    }
    
     
    void addAppointmenRecords( )
    {  
            int number, i;
    
    system("cls");         //clear screen
    
    	cout<<"\nHow many Appointmen you wish to add? ";
    	cin>>number;
    	cin.get();                         //read newline character left in the buffer
    
    	if( (number + currentSize ) <= listSize)                   //There is still room in the array	
    	      for( i = 1; i<=number; i++)
               {
    			cout<<"\nEnter Person name: ";
    			getline(cin, AppointmenList[currentSize].name);
    			cout<<"Enter Appointmen Descriptions: ";
    			getline(cin, AppointmenList[currentSize].description);
    			cout<<"Enter Appointmen date: ";
    			cin>>AppointmenList[currentSize].appdate.day;
    			cin>>AppointmenList[currentSize].appdate.mounth;
    			cin>>AppointmenList[currentSize].appdate.year;
    			cin.get();
    			cout<<"Enter Appointmen time: ";
                getline(cin, AppointmenList[currentSize].time);
    			cout<<endl;
    	  		          
    			currentSize += 1; //update CurrentSize
    		}
    	else
    cout<<"Overflow!!!! Appointmen List is full"<<endl;
             
    
    cout<<"\nPress any key to continue"<<endl;
    cin.get();     //read a character 
    }
    
    void displayHeading( )
    
    {      
        cout<<setiosflags(ios::left);                //left justify output
    	cout<<endl<<setw(20)<<"Person"<<setw(20)<< "Appointment"<<setw(12)<<"Appointment"<<setw(12)<<"\t  Appointment"<<endl;
    	cout<<setw(20)<<"Name"<<setw(20)<< "Descriptions"<<setw(12)<<"Date"<<setw(15)<<"\t  Time\n"<<endl;
    }
    
     
    void displayRecords( )
    {   /* print the data from the array of records under a suitable header*/
        
          int index, option,sdate;
    	  system("cls"); //clear screen
    
    		cout<<"\n\t1. Display For Certein Date"<<endl; //Display the records for certein Date.
    		cout<<"\t2. Display All Appointments"<<endl;
    		cout<<"\nEnter Option Number: ";  //Enter option
    		cin>>option;
    		cin.get();
    
    	if(option == 1){
    		system("cls");  //clear screen
    		cout<<"\nEnter the Date You want To Display: ";
    					cin>>sdate;
    			system("cls"); //clear screen
    			displayHeading( );
    	}
    
    	if (option == 2){
    
    	   displayHeading();
           cout<<setiosflags(ios::left);                //left justify output
    	
    
           for (index = 0; index < currentSize; index++)
    	   {
    	
    		cout<<setw(20)<<AppointmenList[index].name;
    		cout<<setw(15)<<AppointmenList[index].description;
    		cout<<AppointmenList[index].appdate.day;
    		cout<<"/"<<AppointmenList[index].appdate.mounth;
    		cout<<"/"<<setw(12)<<AppointmenList[index].appdate.year;
    		cout<<setw(12)<<AppointmenList[index].time;
    		cout<<endl<<endl;
    	
    	   }
    	   cin.get();
    	}
    		else {
    			cout<<endl<<"Wrong option number!! Try again\n";
    			cin.get();
    				//give the user a chance to read the output data
      		cout<<endl<<"Press any character to continue  ";
    		//cin.get();          //read entered character 
    	}
    }
    void writeAppointmenRecords( )
    {
    	int index;
        //create and open an output text file
    
            ofstream outfile("C:\\Documents and Settings\\zafer\\Desktop\\AppointmenRecords.txt",ios::out | 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
    
    	 ifstream 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();
        
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> im now getting any errors or warnings.
    What does this mean?

    >> if you want you can run it and see yourself thanks.
    Could you just tell us what errors you get (if any). If it compiles, then what is working and what is not? What input do you give it? Why do you think it isn't reading in records?

    One thing I noticed is that in your writeAppointmenRecords function, you write each record on the same line. But in your readAppointmenRecords function you call getline to read in the name. In that situation, getline will read the entire line and get all the data for the entire record in one string and save it to the name. I don't think that's what you want.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    sorry i spelled wrong there im not getting any error
    thats the why i put all my codes so if anyone want to run and see.
    the program asked for user name, Descriptions, date and time for recordes.
    i wrote them on the same line so each person one line recorde.
    its writing to the file like this but when i try to use read function nothing happining.
    Last edited by pczafer; 04-18-2009 at 10:27 AM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> i wrote them on the same line so each person one line recorde.
    You write them on the same line but when you read them you read the whole line into the name. Have you tried to fix that yet?

    Also, normally you don't want to use eof() to control your loop, because eof() doesn't stop the loop until after you try to read and it fails. That will make your code run the loop one extra time. I don't think that's causing your current problem, but you will need to fix it before you get your whole thing working.

    >> thats the why i put all my codes so if anyone want to run and see.
    That's good. But sometimes people don't have compilers available to them. If you provide your input, expected output, and the output you're getting, it really helps people identify the problem. Often we can just look at that information without running the code and help.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    Thanks Daved for replying,
    I made couple of changes, i made every entry differet line
    Code:
    void writeAppointmenRecords( )
    {
    	int index;
        //create and open an output text file
    
            ofstream outfile("C:\\AppointmenRecords.txt",ios::out | 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<<endl;
    		outfile<<AppointmenList[index].description<<endl;
    		outfile<<AppointmenList[index].appdate.day<<endl;
    		outfile<<AppointmenList[index].appdate.mounth<<endl;
    		outfile<<AppointmenList[index].appdate.year<<endl;
    		outfile<<AppointmenList[index].time<<endl<<endl;
    		
    	}  
         //write values to file in an 15 characters field.
    	
        outfile.close( );    // close file
        //cin.get();
    
    }
    
    void readAppointmenRecords( )
    
    {  
    	currentSize=0;
    	int option;
    	//create a stream and open the file 'AppointmenRecords.txt' for input
    
    	 ifstream infile("C:\\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)    
           {
    		infile>>AppointmenList[currentSize].name;
    		infile>>AppointmenList[currentSize].description;
    		infile>>AppointmenList[currentSize].appdate.day;
    		infile>>AppointmenList[currentSize].appdate.mounth;
    		infile>>AppointmenList[currentSize].appdate.year;
    		infile>>AppointmenList[currentSize].time;
    		currentSize+=1;
            }
        infile.close( ); // close file
    	currentSize = currentSize -1;
    	cin.get();
        
    }


    and my entry like this
    Code:
    
    How many Appointmen you wish to add? 2
    
    Enter Person name: zafer
    Enter Appointmen Descriptions: lunch
    Enter Appointmen date: 19 04 2009
    Enter Appointmen time: 12:30
    
    
    Enter Person name: zafer
    Enter Appointmen Descriptions: dinner
    Enter Appointmen date: 19 04 2009
    Enter Appointmen time: 20:30
    writing in to the file like this
    Code:
    zafer
    lunch
    19
    4
    2009
    12:30
    
    zafer
    dinner
    19
    4
    2009
    20:30
    and if i want to see dipslay allAppointmen like this
    Code:
    Person              Appointment         Appointment          Appointment
    Name                Descriptions        Date                 Time
    
    zafer               lunch               19/4/2009             12:30
    
    zafer               dinner              19/4/2009             20:30
    read from file still not giving me any result??????

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

    Thumbs up fixed

    its working now thaks for your help
    Code:
    void writeAppointmenRecords( )
    {
    	int index;
    		//create and open an output text file
    
           ofstream outfile("C:\\Documents and Settings\\zafer\\Desktop\\AppointmenRecords.txt",ios::out | 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();        //read a character         
    	  }
    		
    	for (index=0; index<currentSize; index++)
    	{
            outfile<<AppointmenList[index].name<<endl;
    		outfile<<AppointmenList[index].description<<endl;
    		outfile<<AppointmenList[index].appdate.day<<endl;
    		outfile<<AppointmenList[index].appdate.mounth<<endl;
    		outfile<<AppointmenList[index].appdate.year<<endl;
    		outfile<<AppointmenList[index].time<<endl;
    		
    	}  
         //write values to file in an 15 characters field.
    	
        outfile.close( );    // close file
    }
    
    void readAppointmenRecords( )
    {  
    	//create a stream and open the file 'AppointmenRecords.txt' for input
    
    	 ifstream 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
           {
    			infile>>(infile, AppointmenList[currentSize].name);
    			infile>>(infile, AppointmenList[currentSize].description);
    			infile>>AppointmenList[currentSize].appdate.day;
    			infile>>AppointmenList[currentSize].appdate.mounth;
    			infile>>AppointmenList[currentSize].appdate.year;
    			infile>>(infile, AppointmenList[currentSize].time);
    			currentSize+=1;
            }
    			infile.close( ); // close file
    			currentSize = currentSize -1;
    			cin.get();
        
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Read File Problem
    By lonewolf367 in forum C Programming
    Replies: 11
    Last Post: 11-30-2005, 09:32 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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