ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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?
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!
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.
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).
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;
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.