Hi, I have 2 big binary files that I need to read in a 2d array. So far I have been able to read the first file in buffer array and though I have tried to read the second file in another buffer array it is displaying all 0 values. I do not have much experience in C programming, so any help would be highly appreciated. The code i have written so far is as follows:
insert
Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fileptr1, *fileptr2;
int *buffer1, *buffer2;
long filelen1, filelen2;
//Opening and reading data file en.dat
fileptr1 = fopen("C:\\Users\\ABC\\Desktop\\en.dat", "rb"); // Open the file in binary mode
if(!fileptr1)
{
printf("Unable to open file");
return 1;
}
fseek(fileptr1, 0, SEEK_END); // Jump to the end of the file
filelen1 = ftell(fileptr1); // Get the current byte offset in the file
printf("\nFile length for file EN.dat that is loaded is %d\n\n", filelen1);
rewind(fileptr1); // Jump back to the beginning of the file
buffer1 = (int *)malloc((filelen1+1)*sizeof(int)); // Enough memory for file + \0
fread(buffer1, filelen1, 1, fileptr1); // Read in the entire file
for(int i=0; i<10 ; i++)
{
printf("\n %d = %d", i, buffer1[i]);
}
//Opening and reading xyz.dat
fileptr2 = fopen("C:\\Users\\ABC\\Desktop\\xyz.dat", "rb"); // Open the file in binary mode
if(!fileptr2)
{
printf("Unable to open file");
return 1;
}
fseek(fileptr2, 0, SEEK_END); // Jump to the end of the file
filelen2 = ftell(fileptr2); // Get the current byte offset in the file
printf("\n\n\nFile length for File XYZ.dat that is loaded is %d\n\n", filelen2);
rewind(fileptr2); // Jump back to the beginning of the file
buffer2 = (int *)malloc((filelen2+1)*sizeof(int)); // Enough memory for file + \0
fread(buffer1, filelen2, 1, fileptr2); // Read in the entire file
for(int i=0; i<50 ; i++)
{
printf("\n %d = %d", i, buffer2[i]);
}
//Closing mien.dat and mxyz.dat
fclose(fileptr1); // Close the file
fclose(fileptr2);
}
I am getting correct file lengths but for the second files when I print values i am getting all zeroes.
And how can i load these files into a 2d array?