First off, this isn't homework. Just trying to understand why I'm seeing what I'm seeing on my screen.
The stuff below (my own work) currently takes an input file and reads it as a binary file. I want it to store each byte read in an array (for later use). For the sake of brevity the input file (Hello.txt) just contains 'Hello World', without the apostrophes.
int main(int argc, char *argv[]) {
FILE *input;
int i, size;
int *array;
input = fopen("Hello.txt", "rb");
if (input == NULL) {
perror("Invalid file specified.");
exit(-1);
}
fseek(input, 0, SEEK_END);
size = ftell(input);
fseek(input, 0, SEEK_SET);
array = (int*) malloc(size * sizeof(int));
if (array == NULL) {
perror("Could not allocate array.");
exit(-1);
}
else {
input = fopen("Hello.txt", "rb");
fread(array, sizeof(int), size, input);
// some check on return value of fread?
fclose(input);
}
for (i = 0; i < size; i++) {
printf("array[%d] == %d\n", i, array[i]);
}
Why is it that having the print statement in the for loop as it is above causes the output to look like this
array[0] == 1819043144
array[1] == 1867980911
array[2] == 6581362
array[3] == 0
array[4] == 0
array[5] == 0
array[6] == 0
array[7] == 0
array[8] == 0
array[9] == 0
array[10] == 0
while having it like this
printf("array[%d] == %d\n", i, ((char *)array)[i]);
makes the output look like this (decimal ASCII value for each character)
array[0] == 72
array[1] == 101
array[2] == 108
array[3] == 108
array[4] == 111
array[5] == 32
array[6] == 87
array[7] == 111
array[8] == 114
array[9] == 108
array[10] == 100
? If I'm reading it as a binary file and want to read byte by byte, why don't I get the right ASCII value using the first print statement?
On a related note, what happens if the input file I send in isn't a text document (e.g., jpeg)?
Sorry is this is an entirely trivial matter, but I can't seem to figure out why.
The fread() function returns the number of full items successfully read, which can be less than count if an error occurs, or if the end-of-file is met before reaching count. If size or count is 0, the fread() function returns zero, and the contents of the array and the state of the stream remain unchanged.
fread() is part of the C library, and provides buffered reads.
fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count . Use the feof or ferror function to distinguish a read error from an end-of-file condition.
The behaviour is not surprising:
sizeof(char)
is 1. int
with 11 int. sizeof(int)
is very likely to be 4 on your machinefread
to read up to 11 int
s (up to 44 bytes). So the first 4 characters will be read as an int
and stored in array[0]
and the next 4 in array[1]
.
fread
it would tell you that it actually only read 2 elements (as the content is 11 bytes it can only read 2 int
s and the last 3 remaining bytes cannot be successfully read as an int
).int
you get build up by the first 4 characters.The memory layout basically looks like this:
array[0]
| array[1]
| |
1 2 3 4 5 6 7 8 9 10 11
| |
| ((char *)array)[1]
((char *)array)[0]
Your ftell returns the current value of the position indicator of the stream.
And it returns number of byte the file has. And you are reading file as the sequence of int 4-byte and ofcourse the later element will be 0. For more detail, you are reading 4 x size bytes from a file with size bytes.
Your array should be type of char.
Something like
char* array = malloc(sizeOfFile * sizeof(char));
if(array == NULL) {
...
}
fread(array, sizeOf(char), sizeOfFile, filePointer);
// ..
Just the idea, not the code. Hope this help;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With