afternoondelite
Programmer
I have to create a program with a system call and something is driving me nuts.
For some reason I get really odd results from a file with an odd number of characters:
48 struct stat buffer1;
49 fstat(d1, &buffer1);
50 size_t tot_bytes1 = buffer1.st_size;
51 char *buff1 = malloc(tot_bytes1);
52
53 printf("buff1 len: %d\n", sizeof(buff1));
54
55 ssize_t bytes_read= read(d1, buff1, tot_bytes1);
56
57 printf("tot_bytes: %d\n", (int)tot_bytes1);
58 printf("buff1 len: %d\n", strlen(buff1));
59 printf("bytes_read: %d\n", (int)bytes_read);
60 printf("%s\n", buff1);
buff1 len: 4
tot_bytes: 12
buff1 len: 15
bytes_read: 12
hello world
Ù
And those are the results for one.
And after adding a character to the file:
buff1 len: 4
tot_bytes: 13
buff1 len: 13
bytes_read: 13
hello worlds
For some reason I get really odd results from a file with an odd number of characters:
48 struct stat buffer1;
49 fstat(d1, &buffer1);
50 size_t tot_bytes1 = buffer1.st_size;
51 char *buff1 = malloc(tot_bytes1);
52
53 printf("buff1 len: %d\n", sizeof(buff1));
54
55 ssize_t bytes_read= read(d1, buff1, tot_bytes1);
56
57 printf("tot_bytes: %d\n", (int)tot_bytes1);
58 printf("buff1 len: %d\n", strlen(buff1));
59 printf("bytes_read: %d\n", (int)bytes_read);
60 printf("%s\n", buff1);
buff1 len: 4
tot_bytes: 12
buff1 len: 15
bytes_read: 12
hello world
Ù
And those are the results for one.
And after adding a character to the file:
buff1 len: 4
tot_bytes: 13
buff1 len: 13
bytes_read: 13
hello worlds