I have a person class which writes a name and age to file using binary I/O. By default I need it to create 10 slots for 10 people each set to "null" for name and 0 for age. Then it asks if you want to modify a record and if so which one. First, I need help setting my char array defined in my class to "null" within the default constructor. Second I need to know why my "for" loop for creating the 10 slots isn't working. Code is below

class person                      //class of persons
 {
   protected:
      char name[80];           //person's name
      short age;                  //person's age
   public:
      void getData()              //get person's data
         {
         cout << "Enter name: "; cin >> name;
         cout << "Enter age: "; cin >> age;
         };
	  person();
};


int main()
{
	char again;
	int num;
	person pers;                   //create a person
	int position;
   
                                 
    ofstream outfile("persons.txt", ios::binary);
                                  
	outfile.seekp(0);
	for(int i = 0; i < 9; i++)
	{
		outfile.write((char*)(&pers), sizeof(pers)); 
	}

	cout << "Would you like to modify a record? (y or n): ";
	cin >> again;

	while (again == 'y')
	{
		cout << "Which record would you like to modify? (1-10): ";
		cin >> num;
		position = (num-1) * sizeof(pers);
		outfile.seekp(position);
		pers.getData();
		outfile.write((char*)(&pers), sizeof(pers));
		cout << "Would you like to modify another record? (y or n): ";
		cin >> again;
	}   

	_getch();
	return 0;
}

Recommended Answers

All 3 Replies

>> First, I need help setting my char array defined in my class to "null" within the default constructor.

That's an easy one. All you do is flood the entire buffer with 0s.

class person                      //class of persons
 {
   protected:
      char name[80];           //person's name
      short age;                  //person's age
   public:
      void getData()              //get person's data
         {
         cout << "Enter name: "; cin >> name;
         cout << "Enter age: "; cin >> age;
         };
        person()
        {
            memset(name,0,sizeof(name));
            age = 0;
       }          
};

>>Second I need to know why my "for" loop for creating the 10 slots isn't working
The loop 0 to 9 is only 9 slots, not ten. The loop counter should count from 0 to 10.

Whenever I try to flood the buffer I don't get a proper txt file (see attached text file). Also the same goes for my for loop. It isn't that it isn't creating enough slots, it just doesn't seem to be working. Does the code inside the loop look correct?

The loop looks ok to me. Remember, it is writing a binary file, not a text file, so you can't view it with programs such as Notepad.exe because they don't know how to read binary files.

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.