Sorry but I'm having a problem whit this function that is meant to let me edit records in a binary file. After I enter what I want to change an attribute to the file remains unchanged.

void updateStudent(fstream & file)
{
     S students;
     long lenth;                              
     char snum[10];
     char editChoice = '7';
     bool editmade = 0; 
     lenth =  sizeof(S);
     int list = 1;
     int recno = 0;
     
     file.open(FILENAME, ios::in|ios::out|ios::binary);
          
     if(!file)
     {
      cout<<"can not open file"<<endl;         
     }
      
     //file.clear();  //refresh file variable
     file.seekg (0,ios:: beg);
     
     file.read( reinterpret_cast< char *> (&students),sizeof(S));
     
     while(!file.eof())
     {
		cout<<"Enter "<<list<<" to edit \t"<<students.regNumber<<endl;
		file.read( reinterpret_cast< char *> (&students),sizeof(S));
		list++;
     }
       list--;

     
     cout << "Enter number 1 to "<<list<<" of student to be found:"<<endl;
     
     
     cin.getline(snum, 10);
     //cin.ignore();
     
     
     
     file.clear();  //refresh file variable     
     file.seekg(0, ios::beg); 

     //file.read( reinterpret_cast< char *> (&students),sizeof(S));
         
     
     
     while(!(file.eof()) && editChoice!='Q')
     {
     
		if (strcmp(snum, students.regNumber) != 1)
		{
		   do {                   
				updateMenu (editChoice);
          
       ///////////////////////////////////////////////////////////////////////////////////////////
          
				switch(editChoice)
          
				{
                    cout<<editChoice;
                case '1':{
                    cout<<"Editing student number"<<endl;
                    cout<<"Enter the registration number."<<endl;
                    //cin>>students.regNumber;
                    
                    cin.getline(students.regNumber, 10);
                    
                     //client.accountNumber - 1 ) *       // position 
                     //sizeof( clientData )
                    
                    int pos = file.tellg();
                    int offset = pos-10;
                    
                    
                    file.seekp( offset, ios::beg ); 
                    file.write( reinterpret_cast< char *> (&students),sizeof(S));
                    editChoice = '0';
                    editmade = 1;
					break;
                }
                   
                case '2':{
					cout<<"edit student name"<<endl;
                          
                    break;
                }
                          
                case '3':{
                    cout<<"To edit date registered"<<endl;
                     
                    break;
                }
                          
                case '4':{
                    cout<<"edit modules"<<endl;
                          
                    break;
                }
                          
                case 'Q':{
                    cout<<"About to Quit"<<endl;
                    file.eof();
                    break;
                }
                            
                default: {
                    cout<<"Enter A,B,C,D,E or Q"<<endl;
                    break;
                }
				}

			
				
			} while( editChoice!='Q');
	
	

	
		}
		else{
		cout<<"not a student"<<endl;
		
	}
    file.close(); 
	}
}

Sorry about the messy code.

Thanks.

Recommended Answers

All 2 Replies

line 51: strcmp() returns 0 when the two strings are the same.

Thanks but I'm still having the same problem.

void updateStudent(fstream & file)
{
     S students;
     long lenth;                              
     char snum[10];
     char editChoice = '7';
     bool editmade = 0; 
     lenth =  sizeof(S);
     int list = 1;
     int recno = 0;
     
     file.open(FILENAME, ios::in|ios::out|ios::binary);
          
     if(!file)
     {
      cout<<"can not open file"<<endl;         
     }
      
     //file.clear();  //refresh file variable
     file.seekg (0,ios:: beg);
     
     file.read( reinterpret_cast< char *> (&students),sizeof(S));
     
     while(!file.eof())
     {
		cout<<"Enter "<<list<<" to edit \t"<<students.regNumber<<endl;
		file.read( reinterpret_cast< char *> (&students),sizeof(S));
		list++;
     }
       list--;

     cin.ignore();
     cout << "Enter number of the student to be found:"<<endl;
     cin.getline(snum, 10);
     cin.ignore();
     
     
     
     file.clear();  //refresh file variable     
     file.seekg(0, ios::beg); 

     //file.read( reinterpret_cast< char *> (&students),sizeof(S));
         
     
     
     while(!(file.eof()) && editChoice!='Q')
     {
     
		if (strcmp(snum, students.regNumber) == 0)
		{
		   do {                   
				updateMenu (editChoice);
          
       ///////////////////////////////////////////////////////////////////////////////////////////
          
				switch(editChoice)
          
				{
                    cout<<editChoice;
                case '1':{
                    cout<<"Editing student number"<<endl;
                    cout<<"Enter the registration number."<<endl;
                    //cin>>students.regNumber;
                    
                    cin.getline(students.regNumber, 10);
                    
                     //client.accountNumber - 1 ) *       // position 
                     //sizeof( clientData )
                    
                    int pos = file.tellg();
                    int offset = pos-10;
                    
                    
                    file.seekp( offset, ios::beg ); 
                    file.write( reinterpret_cast< char *> (&students),sizeof(S));
                    editChoice = '0';
                    editmade = 1;
					break;
                }
                   
                case '2':{
					cout<<"edit student name"<<endl;
                          
                    break;
                }
                          
                case '3':{
                    cout<<"To edit date registered"<<endl;
                     
                    break;
                }
                          
                case '4':{
                    cout<<"edit modules"<<endl;
                          
                    break;
                }
                          
                case 'Q':{
                    cout<<"About to Quit"<<endl;
                    file.eof();
                    break;
                }
                            
                default: {
                    cout<<"Enter A,B,C,D,E or Q"<<endl;
                    break;
                }
				}

			
				
			} while( editChoice!='Q');
	
	

	
		}
		
		else{
		cout<<"not a student"<<endl;
		file.read( reinterpret_cast< char *> (&students),sizeof(S));
	    }
    file.close(); 
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.