I don't see where I'm clobbering the memory

C

Chad

my input file is:
test

my program:
#include <stdio.h>
#include <stdlib.h>

#define LOG "/home/miss_xtc/flood/words2.txt"

#define BUFF 20

struct data {
char val[BUFF];
};

int main(void) {

struct data profanity;
FILE *fp;

if ((fp = fopen(LOG,"r")) == NULL){
perror("error in load_data function:");
exit(1);
}

while(fread(&profanity,sizeof(struct data),1,fp) == 1) {
/*fwrite(&profanity,sizeof(struct data),1,fp);*/

}

printf("%s\n", profanity.val);


if(ferror(fp)){
perror("can't read file: ");
}
fclose(fp);

return 0;
}


the output:
miss_xtc@linux:~/flood> gcc -g -Wall beta2.c -o beta2 -lefence
miss_xtc@linux:~/flood> ./beta2

Electric Fence 2.2.0 Copyright (C) 1987-1999 Bruce Perens
<[email protected]>
test??<??@0?
miss_xtc@linux:~/flood>

Chad
 
C

Chad

Tom said:
Bad idea for portable code.


You have no room for the NUL char.

If my input file is has the words "test" , this is 4 chars plus one for
the NULL. The size of BUFF is
20. How can I have no room for NULL in this case?

Chad
 
R

Richard Heathfield

Chad said:
If my input file is has the words "test" , this is 4 chars plus one for
the NULL.

He said NUL (a null character), not NULL (a null pointer constant).
The size of BUFF is
20. How can I have no room for NULL in this case?

Oh, if you are only storing 19 or fewer characters, then there's plenty of
room, but I didn't quite see where you wrote a null character into your
character array. Perhaps you forgot?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
G

Gordon Burditt

The problem is that you're NOT clobbering memory.
If my input file is has the words "test" , this is 4 chars plus one for
the NULL. The size of BUFF is
20. How can I have no room for NULL in this case?

You have room for the '\0' (not NULL, NULL is for pointers) char.
So why didn't you put one there? fread() does not guarantee
termination of the buffer (unless there's a '\0' character in the
file, which is unusual in text files), even if it's not totally
filled.
 
O

Old Wolf

Chad said:
while(fread(&profanity,sizeof(struct data),1,fp) == 1) {
/*fwrite(&profanity,sizeof(struct data),1,fp);*/

}

printf("%s\n", profanity.val);

printf expects some characters followed by a zero-valued byte
which indicates the end of them. Text files don't contain such
bytes -- and fread just gets what was in the file. If you want to
pass such things to printf then you will need to manually
place in the zero-byte at the appropriate place.

Also, your program will behave poorly if the first fread fails.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top