Thread: Reading binary files

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    20

    Reading binary files

    First post here, please bare with me.

    My professor has 2 binary files of matrices on his course website. I must write code to manipulate these matrices etc. I have coded matrix operations before, so that is not the problem.

    My problem is, I am unsure of how to read a binary file either found online or saved to my computer, and then once I have "read" this file, how I should then put this data into a 2 dimensional array or another form of storage. Perhaps the second part of the question may be self explanatory depending on how the reading of a binary file may work. Anyways, any and all comments are welcome, thanks in advance.

    EDIT: Just realized I forgot to supply a piece of information. The first two numbers in the binary file are the number of rows and the number of columns, and all subsequent numbers are simply the elements of the matrix.
    Last edited by rlesko; 11-09-2010 at 03:20 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    I/O on binary files is done by fread() / fwrite(). The first call to fread() gets 2 ints worth of data (row and column). Now malloc() storage for the 2D array with those dimensions. Subsequent calls to fread() get no. of ints from the binary file equal to the no. of columns instead of just two.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    Sorry I have no updated this in a while, I got carried way with other things. Anyways, I keep getting a segmentation fault when I run my program. Here is my code:

    Code:
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    
    
    int main (int argc, char *argv)
    {
    FILE *fp;
    float q;
    int m;
    fp=fopen("C:\\Users\\MyName\\Desktop\\Matrices\\matrixA.bin", "rb");
    m=fread(&q, sizeof(q), 1, fp);
    printf("\n%f and %i \n\n", q,m);
    
    
    fclose(fp);
    
    return 0;
    }
    Any idea whats wrong? And just to clarify, I said I was reading in two bytes before (which I still am going to need to do) but for now I am just doing one.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You need to test fp to make sure the file is opening correctly. If it doesn't you'll be asking your program to read from a null pointer....

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    How do I test for that? I've tried isolating different lines of code throughout the program, and the fread line is the only one that gives a segmentation fault when I run it. Could there be something wrong with the way I am reading the file from the folder on my desktop?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    So I just wrote a quick little if statement after I try to assign the *fp a value to check if it still was equal to NULL and it was. So something is wrong with my fopen statement but I have no idea what. Any suggestions would be great. Thank you.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's strange that you include unistd.h (a file NOT found on windows systems), yet you're using C:\\ in your filename.

    Code:
    if ( fp == NULL ) {
        perror("Help");
    }
    What else does it print, apart from "Help"?
    File not found perhaps?
    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.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    I am programming on a UNIX server which is why I included unistd.h. It prints "No such file or directory." I have also tried using a web address where the binary file is located as a download and I get the same error message. I suppose after further thought it would make sense for me to set up some sort of ftp to my computer, but I still don't know why the web address wouldn't work. Any suggestions? Thank you.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You speak of web address but your file name is a simple path name.
    The fopen should work unless the file doesn’t exist (or doesn’t have read permission, or is locked, etc.)

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    Quote Originally Posted by nonoob View Post
    You speak of web address but your file name is a simple path name.
    The fopen should work unless the file doesn’t exist (or doesn’t have read permission, or is locked, etc.)
    Yes I know I have a pathname in my code, I was just saying I tried a web address as well.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    So you want it to work for web address? That would take more than what an fopen can do as far as I know. Sorry I can't help you there.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rlesko View Post
    How do I test for that? I've tried isolating different lines of code throughout the program, and the fread line is the only one that gives a segmentation fault when I run it. Could there be something wrong with the way I am reading the file from the folder on my desktop?
    Take a look at the return values from fopen() and other file handling functions...
    They all have special cases for when the function fails...

    Code:
    fp = fopen(FileName,"r");
    if (fp == 0)
      { printf(" Could not open %s \n",FileName);
         exit (0); }
    Also ... if you're programing on linux or unix, you are using the wrong slash in your filenames....
    Last edited by CommonTater; 11-23-2010 at 03:14 PM.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    I have tried slashes both ways, using both one and two slashes. I have no idea how to read this file on my system from this UNIX server. Any and all suggestions are greatly appreciated.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Ask you tutor where the files are stored.

    For example, from your UNIX shell prompt, try
    pwd
    ls

    Typically, your path should begin with something like
    /home/user

    It certainly won't be
    C:/users/user
    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. reading and writing to a binary file
    By greg677 in forum C Programming
    Replies: 2
    Last Post: 05-24-2010, 08:09 PM
  2. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  3. Reading Binary files
    By earth_angel in forum C++ Programming
    Replies: 10
    Last Post: 07-12-2005, 06:48 AM
  4. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 11:58 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM