Binary I/O

The program i've created is essentially a calendar. You add events, delete, search for etc. when you close out the program, it opens a txt file and writes the events into it in binary. When you open the program again it loads the events from the same file. Now because it reads/writes in binary i don't know if the problem is in the reading or writing process but essentially..some data is missing when it loads the file.

An 'event' consists of a date(3 integers), category(string), and description(string). The category and description are blank when loaded back into the program. Below is the code for the reading and writing portions as well as what the output should look like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void loadList()
   {
   ifstream inFile;
   Event current;
   
   inFile.open("Events.txt", ios::in | ios::binary);
   if(inFile.fail())
      {
      cout << "\nCannot open file 'Events.txt'!\n\n";
      system("pause");
      return;
      }
      
   inFile.read((char*) &current, sizeof(Event));
   while(!inFile.eof())
      {
      Array[CD.getCount()] = current;
      CD.plusCount();
      CD.plusLast();
      inFile.read((char*) &current, sizeof(Event));                
      }
   inFile.close();
   }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void saveList()
   {
   ofstream outFile;
   
   outFile.open("Events.txt", ios::binary);
   if(outFile.fail())
      {
      cout << "\nCannot open file 'Events.txt'!\n";
      system("pause");
      return;
      }
      
   //write to file
   for(int i = 0; i <=CD.getLast(); i++)
      {
      if(Array[i].getDay() == 0 && Array[i].getMonth() == 0);
      else
         outFile.write((char*) &Array[i], sizeof(Event));     
      }  
   outFile.close();
   }


When loaded, the output should look like this:

Date of event: 12/25/2008
Category: Holiday
Description: Christmas

but it's showing up as follows:

Date of event: 12/25/2008
Category:
Description:


Any ideas?
When you open the file after it has been saved in notepad can you see the Category and Description in amongst the binary data?
closed account (z05DSL3A)
CROSS POSTED:
http://www.cplusplus.com/forum/general/2417/
No...there are absolutely no distinguishable characters in the binary code within the file.
Topic archived. No new replies allowed.