Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 117,577 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 2,157 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Binary Files and strings with numbers

 
Reply to this topicStart new topic

Binary Files and strings with numbers, Binary Files

cdavidsn
post 22 Jul, 2008 - 05:56 AM
Post #1


New D.I.C Head

*
Joined: 21 Jul, 2008
Posts: 5

OK. I have a really strange issue. I am writing a program to write and read binary files. When I have a string that has a number at the end I am getting some weird outputs. I have a string sequence like "dtestja6". When I read that string back from the file I am getting "dtestja6Di0". I think it has something to do with the fact that the last character is a number. I can write strings without a number and I have no issues. I am not sure how to resolve the issue. It seems like there might be a bug here with C++. Has anyone else experienced issues like this with binary files and strings with numbers? Any help would be appreciated.

User is offlineProfile CardPM

Go to the top of the page


AmitTheInfinity
post 22 Jul, 2008 - 06:13 AM
Post #2


D.I.C Addict

Group Icon
Joined: 25 Jan, 2007
Posts: 803



Thanked 17 times

Dream Kudos: 125
My Contributions


QUOTE(cdavidsn @ 22 Jul, 2008 - 06:26 PM) *

OK. I have a really strange issue. I am writing a program to write and read binary files. When I have a string that has a number at the end I am getting some weird outputs. I have a string sequence like "dtestja6". When I read that string back from the file I am getting "dtestja6Di0". I think it has something to do with the fact that the last character is a number. I can write strings without a number and I have no issues. I am not sure how to resolve the issue. It seems like there might be a bug here with C++. Has anyone else experienced issues like this with binary files and strings with numbers? Any help would be appreciated.



It would be lot easier to help you if you post your code here.
User is offlineProfile CardPM

Go to the top of the page

GhostlyDeath
post 22 Jul, 2008 - 06:54 AM
Post #3


New D.I.C Head

*
Joined: 11 Jul, 2008
Posts: 15


My Contributions


Might be a missing terminating 0
User is offlineProfile CardPM

Go to the top of the page

My_code
post 22 Jul, 2008 - 07:02 AM
Post #4


New D.I.C Head

*
Joined: 10 Jul, 2008
Posts: 13

Can you post the code so that it will be helpful.
User is offlineProfile CardPM

Go to the top of the page

cdavidsn
post 22 Jul, 2008 - 07:46 AM
Post #5


New D.I.C Head

*
Joined: 21 Jul, 2008
Posts: 5

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.
User is offlineProfile CardPM

Go to the top of the page

cdavidsn
post 22 Jul, 2008 - 01:33 PM
Post #6


New D.I.C Head

*
Joined: 21 Jul, 2008
Posts: 5

OK Real stupid question, how do you add a terminating 0 to a string?
User is offlineProfile CardPM

Go to the top of the page

Einherjar
post 22 Jul, 2008 - 01:40 PM
Post #7


D.I.C Head

**
Joined: 10 Feb, 2008
Posts: 73


My Contributions


QUOTE(cdavidsn @ 22 Jul, 2008 - 04:33 PM) *

OK Real stupid question, how do you add a terminating 0 to a string?


Terminating zero is '\0'
User is offlineProfile CardPM

Go to the top of the page

cdavidsn
post 23 Jul, 2008 - 04:13 AM
Post #8


New D.I.C Head

*
Joined: 21 Jul, 2008
Posts: 5

OK. I am hitting myself on the head several times. I had some issues with the char data types. I was delcaring them forgrtting that they are continous in memory. One of the varibles was sometimes rather close to another in memory ad was overwritting its data and then grabing the left over values at the end. I have gone to useing the string datatype instead. Program is working much better now.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 10/7/08 08:55PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month