Thread: How to read a binary file in 2d array?

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    3

    Post How to read a binary file in 2d array?

    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?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    Are you confusing the notion of a "binary file" with that of a "text file"? I notice that you have the comment "Enough memory for file + \0", but the null terminator used to terminate strings makes no sense at all for a binary file, much less when you are trying to read into an array of ints.

    Perhaps we should start with: what is the file format? As in, how did you get the binary file in the first place?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Shouldn't line #47 be using buffer2 instead of buffer1?
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Mar 2016
    Posts
    3
    Hi..Please ignore that comment line...i used google to help me with this code. That part if you see has been commented by use of "//"

    I do not have any problem handling a text file. I have a binary file with ".dat" extension as in shown in the code. The file was given to me by my teacher who explicitly mentioned it was in binary format.

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    3
    Yes that was a mistake on my part, in line # 47 it is buffer 2 instead of buffer 1

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,656
    The code needs this -> https://en.wikipedia.org/wiki/Indent_style

    Pick an indent style and stick to it.
    Indented code is so much easier for everyone to read.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 08-21-2014, 01:45 PM
  2. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  3. Read a file (binary) into an array of structs
    By Separ in forum C Programming
    Replies: 3
    Last Post: 04-14-2009, 09:09 PM
  4. Read binary file
    By Ken JS in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2007, 11:12 AM

Tags for this Thread