Sorting data in a binary file
Feb 11, 2012 at 2:54pm UTC
hi,
in a game menu for hangman game, I am trying to sort entries in a binary after I delete a user profile to remove the whitespace. My technique was to copy all the data after the deleted user profile into a string and then write it again to the file shifting back by 1 data structure. I am sure about the cursor positions because I am displaying them to check. I am writing the string back but unfortunately when checking the size of string, it only holds 32 bytes of data while a single struct has 48bytes. When I read that struct back I read garbage. below is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
void DeleteUser()
{
PlayerInfo sPlayer = {0};
// show initial contents
fstream finout; // read and write streams
finout.open(pszFileName, ios_base::in | ios_base::out | ios_base::binary);
int ct = 0;
int sCount =0;
if (finout.is_open())
{
finout.seekg(0); // go to beginning
cout << "Here are the current contents of " << pszFileName << endl << endl ;
while (finout.read((char *) &sPlayer, sizeof (sPlayer)))
{
cout << ct++ << ": " << sPlayer.szName << " "
<< sPlayer.nAge << " " << sPlayer.nScore << " "
<< sPlayer.szLastword << " " << endl;
}
if (finout.eof())
finout.clear(); // clear eof flag
else
{
cerr << "Error in reading “ << file << “.\n" ;
exit(EXIT_FAILURE);
}
}
else
{
cerr << pszFileName << "could not be opened -- bye.\n" ;
exit(EXIT_FAILURE);
}
cout << "Please Select a User Profile." << endl << endl;
// change a record
cout << "Enter the User Profile you wish to Delete: " ;
cin >> rec;
eatline(); // get rid of newline
if (rec < 0 || rec >= ct)
{
cerr <<"Invalid User number -- bye\n" ;
exit(EXIT_FAILURE);
}
streampos place = rec * sizeof (sPlayer); // convert to streampos type
finout.seekg(place); // random access
if (finout.fail())
{
cerr << "Error on attempted seek\n" ;
exit(EXIT_FAILURE);
}
finout.read((char *) &sPlayer, sizeof (sPlayer));
cout << "Your selection:\n" ;
cout << rec << ": " << sPlayer.szName << " "
<< sPlayer.nAge << " " << sPlayer.nScore << " "
<< sPlayer.szLastword << " " << endl;
if (finout.eof())
finout.clear(); // clear eof flag
//PlayerInfo dPlayer = {0};
//finout.seekp(place); // go back
//finout.write((char *) &dPlayer, sizeof dPlayer) << flush;
//eatline(); // get rid of newline
//finout.seekp(place); // go back
cout << finout.tellg() << endl;
system("pause" );
int x=0;
string buff;
char tempChar;
while ( !finout.eof() )
{
finout.get(tempChar);
buff += tempChar;
x++;
}
finout.clear(); // clear eof flag
cout << "eof reached" << endl;
cout << "current pos " << finout.tellg() << endl;
cout << buff << endl;
//finout.seekg(48, ios::beg);
finout.seekp(place); // go back
cout << "current pos " << finout.tellg() << endl;
cout << "size of buff: " << (sizeof (buff)) << endl;
cout << "size of x :" << x << endl;
system("pause" );
if (finout.is_open() == true )
{
//oFile.write((char*) &Infowrite, sizeof(Infowrite));
finout.write((char *)&buff, x-1) << flush;
}
else
{
cout << "Failed to open file " << pszFileName << endl;
}
if (finout.fail())
{
cerr << "Error on attempted write" << endl;
exit(EXIT_FAILURE);
}
cout << "oki" << endl;
system("pause" );
SelectUser();
}
Last edited on Feb 11, 2012 at 2:55pm UTC
Feb 11, 2012 at 3:04pm UTC
Trying to delete user data in-place from a file seems like a bad design. Read all the user data, delete/add what you want, then dump it all back to file at once.
Feb 11, 2012 at 3:34pm UTC
Can you specify your method in more step by step pls and what to use to read all data from file (string, char array etc.) and if it is possible to remove just one struct from the whole bunch of data if I put all of it in a string?
Topic archived. No new replies allowed.