7 Replies - 1296 Views - Last Post: 23 July 2008 - 04:13 AM Rate Topic: -----

#1 cdavidsn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-July 08

Binary Files and strings with numbers

Posted 22 July 2008 - 05:56 AM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Binary Files and strings with numbers

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Binary Files and strings with numbers

Posted 22 July 2008 - 06:13 AM

View Postcdavidsn, on 22 Jul, 2008 - 06:26 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#3 GhostlyDeath  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 11-July 08

Re: Binary Files and strings with numbers

Posted 22 July 2008 - 06:54 AM

Might be a missing terminating 0
Was This Post Helpful? 0
  • +
  • -

#4 My_code  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 10-July 08

Re: Binary Files and strings with numbers

Posted 22 July 2008 - 07:02 AM

Can you post the code so that it will be helpful.
Was This Post Helpful? 0
  • +
  • -

#5 cdavidsn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-July 08

Re: Binary Files and strings with numbers

Posted 22 July 2008 - 07:46 AM

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):

//--------------------------------------------------------------------------
/* 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.
Was This Post Helpful? 0
  • +
  • -

#6 cdavidsn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-July 08

Re: Binary Files and strings with numbers

Posted 22 July 2008 - 01:33 PM

OK Real stupid question, how do you add a terminating 0 to a string?
Was This Post Helpful? 0
  • +
  • -

#7 Einherjar  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 10-February 08

Re: Binary Files and strings with numbers

Posted 22 July 2008 - 01:40 PM

View Postcdavidsn, on 22 Jul, 2008 - 04:33 PM, said:

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


Terminating zero is '\0'
Was This Post Helpful? 0
  • +
  • -

#8 cdavidsn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-July 08

Re: Binary Files and strings with numbers

Posted 23 July 2008 - 04:13 AM

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1