Hi, I need a bit of help with this function...
What I'm trying to do is call the function with a string, XOR it with value 255 and save it to the file. If I put a value of 0 to the XOR (leave as is) then it works properly, but if I put any value to XOR the original value with, then it doesn't work: it saves the previous XORed string constantly. Why could it be? I tried cleaning the "string" variable with "string = 0" and "string[0] = 0" but it does the same. Any clues?

Here is the code:

void write(char string[])
{
  int i;
  FILE *file;

  file=fopen("log.txt","ab+");

  if(file!=NULL)
  {
	  for(i=0;i<strlen(string);i++)
		string[i]^=255;
	  fputs(string,file);
  }


  fflush(file);
  fclose(file);

}

Many thanks in advance!

Recommended Answers

All 7 Replies

give it a try with the following

string[i]^=(char)255;

Thanks! Today I woke up and had one of those flashes of "A-haaaa!" and fixed it. It turned out that for some reason (I'm still learning C), the string variable modifies the original variable (the one I was calling the function with) and I was using it later so the value was XORed and hence the problem. Here is the corrected function:

void write(char string[])
{
  int i;
  FILE *file;

  file=fopen("log.txt","ab+");

  if(file!=NULL)
  {
	  for(i=0;i<strlen(string);i++)
		string[i]^=0xff;
	  fputs(string,file);
	  for(i=0;i<strlen(string);i++)
		string[i]^=0xff;
  }


  fflush(file);
  fclose(file);

}

U can do the same as follows:

void write(char string[])
{
  int i;
  FILE *file;
  char *strTemp=(char *)malloc(strlen(string)+1);
  strcpy(strTemp, string);

  file=fopen("log.txt","ab+");

  if(file!=NULL)
  {
	  for(i=0;i<strlen(string);i++)
		strTemp[i]^=0xff;
	  fputs(strTemp,file);
                  free(strTemp);
  }

  fflush(file);
  fclose(file);
}

This way u will save the number of operations done to the string.

Also it would be a good idea to save strlen(string) and instead of doing strlen in the for loop.

int iLen = strlen(string);

for(i = 0; i < iLen; i++)
...

Not only to save operations, but in the non- tempString version you could actually get the wrong length from the modified string.

Many thanks for the suggestions, guys! That'd help me for sure! Really appreciated.

U can do the same as follows:

void write(char string[])
{
  int i;
  FILE *file;
  char *strTemp=(char *)malloc(strlen(string)+1);
/*
 * Where would be that pointer be pointing to if malloc is not successful
* allocating memory? You must always check for the return of malloc.
* casting malloc is not necessary if you include the proper stdlib.h header file
*/
  strcpy(strTemp, string); /* might be writing where there's no memory available, creating segmentation fault */

  file=fopen("log.txt","ab+");
/* 
 * You check if the file was opened successfully, but you do not provide
 * a solution if the file couldn't be opened properly
*/

  if(file!=NULL)
  {
	  for(i=0;i<strlen(string);i++)
		strTemp[i]^=0xff;
	  fputs(strTemp,file);
                  free(strTemp); /* If the file was not opened, you never released the memory allocated before going into the if statement, creating a memory leak */
  }

  fflush(file);
  fclose(file); /* you might be closing a file that it was never opened */
}

This way u will save the number of operations done to the string.

It is best if code is as proper as possible when posting to show how's done.

It is best if code is as proper as possible when posting to show how's done.

thanks for that suggestion. I will keep those in mind.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.