can't get all of excute file 's content

First,sorry about my bad english.
I try to get content of excute file on linux.But there is a error.
instead of displaying all of content.It only display a part of them

Myfile is b.out :
quan@quan-desktop:~/quanrocktest/tuan1$ cat b.out
�ELFquan���0�4 4($!44�4�44�4�����
....
...
�4�44�4
etc...
b.out is an ELF file

Mycode :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <stdlib.h>

int main(){

		FILE *file;
		char *buffer;
		unsigned int fileLen;

		//Open file
		file = fopen("/home/quan/quanrocktest/a/b.out", "r");

		//Get file length
		fseek(file, 0, SEEK_END);
		fileLen=ftell(file);
		fseek(file, 0, SEEK_SET);

		//Allocate memory
		buffer=(char *)malloc(fileLen+1);
		if (!buffer)
		{
			fprintf(stderr, "Memory error!");
	                                fclose(file);
			return 0;
		}

		//Read file contents into buffer
		fread(buffer, fileLen, 1, file);
		fclose(file);
		printf("len is %d \n",fileLen);
		//Do what ever with buffer
		printf("%s",buffer);
		free(buffer);
return 0;
}


And result :
quan@quan-desktop:~/quanrocktest/a$ g++ init.c
quan@quan-desktop:~/quanrocktest/a$ ./a.out
len is 8256
�ELFquan���@quan-desktop:~/quanrocktest/a$

a.out just display �ELFquan���
When i use vi or cat,b.out still display well
Last edited on
Your string has '\0' in the middle.
cat probably makes a binary copy.
The problem is definitely that you're not opening the file in binary ("rb") mode.
Topic archived. No new replies allowed.