Thread: Help me create a binary file read routine using C or C++

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    Question Help me create a binary file read routine using C or C++

    Hi everyone
    Please bear with this beginner in C/C++ question....

    Problem:
    I need to develop simple program for canny edge detector.
    Now, I need help to create a routine to read a binary file. The routine must be able to read a binary file and pass these values:
    a) first 2 bytes as unsigned integer (assign it to "row" variable)
    b)next 2 bytes as unsigned integer (assign it "col" variable)
    skip 6 bytes
    e) a 2 dimensional array ( data[row][colum] ) from the subsquent bytes left, 1 byte makes up 1 element.
    You can use fread or fstream for this routine.

    ex: using hexview, the input file is:
    02 00 02 00 FF 01 02 01 00 01 11 FF 22 44
    then the program
    should return
    row = 2 (from 00 02-1st 2 bytes)
    col = 2 (from (00 02 - 1st 2 bytes)
    skip the next 6 bytes (FF 01 02 01 00 01)
    data[row][column]= {(17,255)(34,68)} (the rest bytes)

    My current program as follow
    Code:
    #include <stdio.h>
    #include <iostream>
    void infile(char i){
    FILE* input = fopen(i, "rb");
    int col, row;
    double data[600][600];
    fclose(input);
    }
    int main(){
    char i_f;
    printf("Please enter input image file name");
    cin >> i_f;
    infile (i_f);
    }

    I also have the problem to pass the file input from the main program to the routines with above codes. Seems the parameter for char i mismatch or something.
    Could anyone help me to solve this?

    Thx for the help.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Please indent your code, as otherwise everyone (including you) will have a hard time reading the code.
    Since you are using C++, fopen/fclose is bad.
    Learn about file streams in C++. Ifstream's read can read binary data. You can specify how many bytes to read.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    Err, pardon me for the lack in indentation, I posted it in a hurry.

    Yep, I do realize that using fopen or fclose is not proper for C++, instead fstream is preferable.
    Actually I did do some search from the net before posting the question to this forum. I've look at examples provided. But, I'm still clueless as all of the examples I find are not so specific.

    I'll really appreciate if you could instead provide me with the solution or maybe pointer to sites that give specific examples. Before the flaming starts, I do realize that solution given directly is a bad practice as spoon feeding, but for my case as I'll need to expand the code more and more later on to practice the canny detector, I believe spoon feeding case is irrelevant. Not to mention, most of the programmers considered this as basic and not so hard to do....

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to read from a file, you could do
    Code:
    int n1, n2;
    char buf[1000];
    std::istream myfile("my file.txt");
    // Read 2 bytes from file, store in n1/2.
    myfile.read((char*)&n1, 2);
    myfile.read((char*)&n2, 2);
    // Read rest of file
    while ( myfile.read(buf, 1000) );
    This is small example of how to do things.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Read and write binary file?
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2008, 05:31 PM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM