tags:

views:

78

answers:

3

Hi my code is as follows

char name[100] ;
_getcwd(name, (size_t)sizeOfFileName);
strcat(name,"\\") ;
strcat(name, fileName) ;
char *value_str= NULL ;
file = fopen(name, "a+");
if(!file)
   printf("bad file name") ;
for(i = 0; i<fileSize ; i++)
 {
  value_str = fp_to_str(ddata[i]) ;
  strLength= strlen(value_str) ;
  value_str[strLength+1] = 10 ;
  num = strlen(value_str);
  count = fwrite(value_str, sizeof(char), num, file);
 }

I'm having problems writing the value of value_str into the file given by file. Until it reaches the line of code containing fwrite, the value of value_str does not change. then it changes to some junk value. Can you please tell me what is wrong? SizeOfFileName is passed to the function earlier. the code works perfectly until it reaches the fwrite line of code and then its value just changes. and some junk values are written into the text file.

the function fp_to_str is my own function in the current code. it properly returns the value i want to value_str. what fp_to_str does is to convert a double number into a character array, which is stored in value_str.

once the code is done with the fwrite, it changes the correct data that is there in value_str to some garbage value.

ddata[i] takes the double number from the double array one by one and passes it into fp_to_str(). what i am trying to do is write these double numbers which are in ddata[] array into a text file.

I have commented the line to add value 10 to the end of the string. I assumed wrongly that I have to add a null character at the end of the string. I still am having the same problem. anyone know why?

A: 

First, I'd fix the _getcwd line to pass the length of the buffer you're actually using:

_getcwd(name, sizeof(name));

You also have to be sure that your strcats don't cause a buffer overrun.

I'm not familiar with fp_to_str, which doesn't appear to be a standard function. Given that you're not freeing the memory it returns, I'm assuming it returns a pointer to a static buffer.

When you try to add value 10 (line feed?) to the end of the buffer, you're possibly writing beyond the end of the buffer. That's a buffer overrun. I don't see how this particular buffer overrun could clobber other variables on the stack, but I'd fix it and see what symptoms remain.

Adrian McCarthy
"When you try to add value 10 (line feed?) to the end of the buffer, you're wiping out the terminating null character." Thanatos is right here. He's actually modifying the character after the terminator.
Matthew Flaschen
@Matthew: Ah, your right. I missed the +1. It's still a possible buffer overrun, since he hasn't given us information about the buffer that `fp_to_str` returns. While I did miss a small detail, I'm not sure I was modded down for pointing out two possible buffer overruns.
Adrian McCarthy
@Adrian, I didn't vote you down. However, you should correct your answer; the write to `value_str` may be a buffer overflow by one char, but since he's not overwriting the terminator, the `fwrite` shouldn't overflow.
Matthew Flaschen
+1  A: 

In addition to what Adrian McCarthy said:

value_str = fp_to_str(ddata[i]) ;
strLength= strlen(value_str) ;
value_str[strLength+1] = 10 ;
num = strlen(value_str);

num will be equal to strLength here, which makes me think you're not sure as to what you're doing. (What are you doing?) You've add the value 10 (Magic constant - is this a newline? Use '\n') to the space after the null terminator. Your string is:

o  W  o  r  l  d  \0 \x10
Thanatos
A: 

If value_str does not change until the line containing fwrite, then fp_to_str must be returning NULL (since the pointer was initialized to NULL and you report that that value doesn't change). Is NULL a valid return value from fp_to_str, or might that indicate an error?

bta