Code is as follows (Sorry about not including code, I was of hope that this has happened to someone before and might be able to tell me about it):
cpp
//--------------------------------------------------------------------------
/* Function Name: makeTheFile
* Purpose - To make the log file when the first user log in
* Arguments: nameOfFile - type char used to hold the name of the log file
* newUser5and2 - user id of the person making the file
* Returns - 1 for success 0 for failure
*/
//-------------------------------------------------------------------------
int makeTheFile(char *nameOfFile, char *newUser5And2)
{
char *localFileName;
int sizeUserID;
int numOfUsers = 1;
char *User5And2Var;
std:ostringstream stm;
sizeUserID = strlen(newUser5And2);
if ((User5And2Var = new char[sizeUserID]) == NULL)
{
cout << "Could Not Allocate Array." << endl;
return(1);
}
strcpy(User5And2Var,newUser5And2);
ofstream binary_file(nameOfFile,ios::out|ios::binary);
binary_file.write(reinterpret_cast<char *>(&numOfUsers),sizeof(int));
binary_file.write(reinterpret_cast<char *>(&sizeUserID),sizeof(int));
binary_file.write((char *) (User5And2Var),sizeof(char) * sizeUserID);
binary_file.close();
}
//---------------------------------------------------------------------------
/*Function Name: appendNameToFile
* Purpose - To add the new user to the log file. It will read the whole file
* into a linked list. It will delete the file and then regenerate the file
* with the name added to the list.
* Arguments: NameOfFile - Type char used to hold the name of the log file
* NewUser5And2 - user id of the person being added.
* Returns: 1 for failure 0 for success
*/
//--------------------------------------------------------------------------
int appendNameToFile(char *nameOfFile, char *newUser5And2)
{
char *localFileName;
char *readUserID;
int readloop;
int numOfUsers;
int lengthOfUserID;
int headOfFile;
struct node *HeadNode = new node;
struct node *userName;
struct node *tailNode;
struct fileData *dataForFile;
std:ostringstream stm;
headOfFile = 0;
ifstream binary_file(nameOfFile,ios::in|ios::binary); //Open file for reading
binary_file.read(reinterpret_cast<char *>(&numOfUsers),sizeof(int));
if ((tailNode = new(node)) == NULL)
{
cout << "Out of Space!!!" << endl;
return(1);
}
HeadNode->next = NULL;
tailNode = HeadNode;
if ((userName = new(node))== NULL)
{
cout << "Out of Space!!" << endl;
return(1);
}
tailNode->next = userName;
userName->users5And2 = newUser5And2;
userName->next = NULL;
tailNode = userName;
for (int readloop = 0;readloop < numOfUsers;readloop++)
{
binary_file.read(reinterpret_cast<char *>(&lengthOfUserID),sizeof(int));
if ((userName = new(node)) == NULL)
{
cout << "Out of Space!!!" << endl;
return(1);
}
//binary_file.read((char *) (readUserID),sizeof(char) * lengthOfUserID);
binary_file.read((char *) (readUserID),lengthOfUserID);
cout << readUserID << endl; //----------------------------------This is where I print the info out and I get the strange results
userName->users5And2 = readUserID;
userName->next = NULL;
tailNode->next = userName;
tailNode = userName;
}
binary_file.close();
remove(nameOfFile);
ofstream binary_file2(nameOfFile,ios::out|ios::binary);
binary_file2.write(reinterpret_cast<char *>(&numOfUsers + 1),sizeof(int));
userName = HeadNode->next;
while (userName->next != NULL)
{
lengthOfUserID = sizeof(userName->users5And2);
binary_file2.write(reinterpret_cast<char *>(&lengthOfUserID),sizeof(int));
//binary_file2.write((char *) (readUserID),sizeof(char)*lengthOfUserID);
binary_file2.write((char *) (userName->users5And2),sizeof(char)*lengthOfUserID);
HeadNode->next = userName->next;
tailNode = userName;
userName = userName->next;
delete tailNode;
}
delete HeadNode;
}
The whole purpose of this program is to track users who log into some of our Access Databases. I am using C++ to keep my hand in the
language. I took a course a little over a year ago and I thought it would be a good idea so I can keep what I learned sort of fresh in my mind.
I do not think there is much of a problem in my code. I am still working on the link list and getting that to work right. I am sure I can get that to work
like I want. Its just that sometimes when I write information to the bimary file I am getting some strange characters at the end when the last
character is a number. The funny thing is it seems to happen only with some combinations of text and numbers.