LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2003, 06:16 PM   #1
trixar
LQ Newbie
 
Registered: Dec 2003
Posts: 7

Rep: Reputation: 0
open binary files in c/c++


HI
I want to open a binary file and read from it using the read() function. if i use read(test, *buf, 1000)
buf will only read the first 2 or 3 bytes, why?
 
Old 12-14-2003, 07:26 PM   #2
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Rep: Reputation: 30
Re: open binary files in c/c++

Quote:
Originally posted by trixar
HI
I want to open a binary file and read from it using the read() function. if i use read(test, *buf, 1000)
buf will only read the first 2 or 3 bytes, why?
Can you show a little more code?

Tarts
 
Old 12-15-2003, 05:01 AM   #3
trixar
LQ Newbie
 
Registered: Dec 2003
Posts: 7

Original Poster
Rep: Reputation: 0
....
char buf[1000];

if ((handle = open("binaryfile",O_RDONLY)) ==-1)
{
perror();
exit(1);
}

read (handle, buf, 900);

cout <<buf;

when executing:
./open

MZ

and done;
 
Old 12-15-2003, 05:44 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
try:
Code:
if ((handle = open("binaryfile",O_RDONLY | O_BINARY)) ==-1)
 
Old 12-16-2003, 06:34 AM   #5
vasudevadas
Member
 
Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

Rep: Reputation: 30
Don't use read(). That's meant for reading ASCII data, like you would from stdin - it reads until it gets a newline character and then returns.

If you're loading a binary file, use fread() instead:

Code:
  size_t fread(
          void *pointer,
          size_t *size,
          size_t *num_items,
          FILE *stream );
pointer points to your array, size should be set to 1, num_items should be set to the size of the file you are loading (in bytes), stream should be set to your file pointer.

If you don't know the size of your file, you can find it out using fstat(). Alternatively, read your file a chunk at a time: set num_items to some arbitrary value, and repeatedly call fread() until it returns a count less than num_items - this means the end of the file has been reached. You would probably need to realloc() your array as you go, and don't forget to increment the parameter you pass to the pointer argument by the number of bytes read so far!
 
Old 09-26-2012, 05:50 PM   #6
kfank
LQ Newbie
 
Registered: Sep 2012
Posts: 1

Rep: Reputation: Disabled
trixar,

Your code is reading binary data correctly. Examine buf with a debugger and you should see all your data. The problem is you are sending a char * to cout and it only reads up to the first 0 byte (standard string).

BTW, O_BINARY is not a valid flag for open().

Alternatively, you could use fopen("binaryfile", "rb") to get a FILE pointer and follow the call presented by vasudevadas.
 
Old 09-26-2012, 08:59 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
1. Some platforms (unices) don't have O_BINARY. Solution:
Code:
#ifndef O_BINARY
#define O_BINARY 0
#endif
2. strlen doesn't work for binary data. Solution: don't use it; read and fread do return the length.

3. Which is better: read or fread? Bad question. They basically do the same thing, read works with file handle (low level); fread, with FILE-pointer (high level).
 
Old 09-27-2012, 06:00 AM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
For C++, don't use fopen(), fread(), etc. Use std::ifstream and related functions.

Note that C and C++ are NOT the same languages.

Code:
#include <fstream>
#include <iostream>

int main()
{
    // attempt to open the file in the proper mode.
    std::ifstream binFile("binaryFile", std::ios::in | std::ios::binary);

    // if the file was successfully open
    if (binFile)
    {
        char buffer[1024];

        // read data from the file until EOF is reached
        while (binFile.read(buffer, sizeof(buffer)))
        {
            // do something with data in buffer
            // the number of bytes actually read can be obtained by
            // examining the value returned by gcount()

            std::cout << binFile.gcount() << " bytes were read from 'binFile'" << std::endl;

            // ...
        }
    }
    else
    {
        std::cerr << "Failed to open 'binaryFile'" << std::endl;
    }
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary Files? Teralon 9 Linux - General 2 02-10-2004 10:36 PM
Blender binary file fails to open adhara Linux - Software 1 12-09-2003 07:35 PM
Binary files sodaforce Linux - Software 1 09-07-2003 05:31 PM
Binary Files guywithredhat Linux - General 7 03-18-2003 06:28 PM
binary files walterw Programming 7 01-27-2003 08:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration