Unix Programming - problem with binary file

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2005 > problem with binary file





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author problem with binary file
niraj.kumar.ait@gmail.com

2005-08-13, 7:49 am


how to read a (whole) binary file in a c++ string .

Is there any terminating character for binary file .EOF doesnt work in
binary file .

When I use EOF it just read ELF char.

ringken@gmail.com

2005-08-13, 5:51 pm

I don't know if it will work in C++,however in C that is ok, but you
should remember EOF is an int value,you can't use:
char ch;
....
if (ch == EOF) ...
you should use "int ch" or it will fail in some condition

Torgny Lyon

2005-08-13, 5:51 pm

niraj.kumar.ait@gmail.com wrote:
>
> how to read a (whole) binary file in a c++ string .


How about read()?

> Is there any terminating character for binary file .EOF doesnt work in
> binary file .


As this is a Unix programming newsgroup, i assume that you mean
"binary" in the sense "executable" and not as opposed to ascii. Whether
an executable has a terminating character or not does of course depend
on the format of the executable (the a.out format does not use a
terminating character). If you use read() you will know that youve
reached eof when it returns 0.

--
Torgny Lyon
SM Ryan

2005-08-13, 5:51 pm

ringken@gmail.com wrote:
# I don't know if it will work in C++,however in C that is ok, but you
# should remember EOF is an int value,you can't use:
# char ch;
# ...
# if (ch == EOF) ...
# you should use "int ch" or it will fail in some condition

fgetc et al return ints not chars. While in the file, they return
nonnegative (0 to 255) character codes. At end of file or error, they
return EOF which is guarenteed distinct from all returned character
codes.

int ch;
while ((ch=fgetc(f))!=EOF) {
ch is the next byte
}
at eof or read error

You can use feof and ferror after a get.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Mention something out of a Charleton Heston movie, and suddenly
everybody's a theology scholar.
joe@invalid.address

2005-08-13, 5:51 pm

niraj.kumar.ait@gmail.com writes:

> how to read a (whole) binary file in a c++ string .
>
> Is there any terminating character for binary file .EOF doesnt work
> in binary file .


Google is your friend, and comp.lang.c++ is probably a better place to
ask this. Try "read entire file c++ string" specifying comp.lang.c++
as the newsgroup to search. I'm guessing the second link "Read entire text
file without using stringstream" will answer your question.

Joe
joe@invalid.address

2005-08-13, 5:51 pm

joe@invalid.address writes:

> niraj.kumar.ait@gmail.com writes:
>
>
> Google is your friend, and comp.lang.c++ is probably a better place
> to ask this. Try "read entire file c++ string" specifying
> comp.lang.c++ as the newsgroup to search. I'm guessing the second
> link "Read entire text file without using stringstream" will answer
> your question.


And yes, I know you said "binary", but there isn't any difference in
comp.unix.programmer.

Joe
Måns Rullgård

2005-08-13, 8:48 pm

joe@invalid.address writes:

> niraj.kumar.ait@gmail.com writes:
>
>
> Google is your friend, and comp.lang.c++ is probably a better place to
> ask this. Try "read entire file c++ string" specifying comp.lang.c++
> as the newsgroup to search. I'm guessing the second link "Read entire text
> file without using stringstream" will answer your question.


The question has already been more than answered in another group.

Please don't multipost.

--
Måns Rullgård
mru@inprovide.com
Gianni Mariani

2005-08-14, 2:49 am

niraj.kumar.ait@gmail.com wrote:
> how to read a (whole) binary file in a c++ string .
>
> Is there any terminating character for binary file .EOF doesnt work in
> binary file .
>
> When I use EOF it just read ELF char.
>


Below is a standard C++ program that will read an entire file
into a std::string.

Reading a file a character at a time is quite compute intensive.

#include <iostream>
#include <fstream>
#include <exception>
#include <vector>
#include <string>

struct readexception
: std::exception
{
std::string m_what;

~readexception() throw()
{
}

readexception( const std::string & i_what )
: m_what( i_what )
{
}

virtual const char* what() const throw()
{
return m_what.c_str();
}
};

std::string readtostring( const char * filename )
{
std::ifstream file( filename, std::ios::binary );

if ( ! file.good() )
{
throw readexception(std::string("Open failed '")+filename+"'");
}

// get length of file:
file.seekg( 0, std::ios::end );

std::streampos length = file.tellg();

file.seekg( 0, std::ios::beg );

// create a vector with all the bytes.
// Unfortunately, std::string is not guarenteed to use contiguous
// storage so to be conforming to the standard we need to copy
// all the bytes from somthing else to a string. std::vector
// is guarenteed to have contiguous bytes so we use std::vector.
std::vector<char> l_buffer( length );

// read data as a block:
std::streamsize l_numread=file.readsome(&l_buffer.front(),length);

if ( l_numread != length )
{
throw readexception(std::string("read failed '")+filename+"'");
}

// finally return the string
return std::string( &l_buffer.front(), l_numread );
}

int main( int argc, char ** argv )
{
if ( argc != 2 )
{
std::cerr << "usage: " << argv[0] << " <filename>\n";
return 1;
}
try {
std::cout << readtostring( argv[1] );
}
catch ( const readexception & l_ex )
{
std::cerr << "readtostring failed " <<l_ex.what()<<std::endl;
return 1;
}

}
Maxim Yegorushkin

2005-08-14, 5:55 pm

niraj.kumar.ait@gmail.com wrote:
> how to read a (whole) binary file in a c++ string .


Don't. Just map the file into memory.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com