So im brand new to C. I need to write a program that takes in a binary file of integers as input, allocate a big enough array with malloc, then use the read() c function to store each integer in the binary file in its respective location in the array. My issue is that I don't know how to differentiate between the two. when I print out the data stored in this allocated array space it just shows the Ascii value of the binary data taken as a string of chars. so for example if I have the binary number 0000 0001, the array from locations [0]-[7] would be each ascii value of each bit in that binary number.
Basically im trying to read each line and store that lines int representation of the binary information in 1 array location, so like the above example the desired outcome would be: [0] - 1. Is there some conversion algorithm I need to do? I believe there to be a way for c to just recognize that its binary info, not ascii because there are 2 different file types: binary and ASCII. Any help would be greatly appreciated, thanks in advance!
P.s I can only use these function calls: open, close, read, malloc, fstat, printf
else
{
fileDescriptor = open(argv[1], O_RDONLY, 0);
if (fileDescriptor == -1){
printf("File Not Found\n");
exit(1);
}
else{
if (fstat(fileDescriptor, &fileStat) < 0){
printf("Trouble pulling statistics of file\n");
exit(1);
}
else
{
numBytes = fileStat.st_size;
filePointer = (char*) malloc(numBytes);
n = read(fileDescriptor, filePointer, (numBytes * sizeof(int)));
if( n < 0 ){
printf("Problem reading the file\n");
exit(1);
}
numerator = 0;
numCount = 0;
average = 0;
while(*filePointer != NULL)
{
numerator += *filePointer;
printf("data in array: %d\n", *filePointer);
filePointer++;
numCount++;
}
average = (double) numerator / (double) numCount;
printf("Average: %f\n", average);
printf("Total Numbers: %d\n", numCount);
}
}