problem with binary file
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > problem with binary file




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-13-05 12:49 PM


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.






[ Post a follow-up to this message ]



    Re: problem with binary file  
ringken@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-13-05 10: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






[ Post a follow-up to this message ]



    Re: problem with binary file  
Torgny Lyon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-13-05 10: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





[ Post a follow-up to this message ]



    Re: problem with binary file  
SM Ryan


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-13-05 10: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.





[ Post a follow-up to this message ]



    Re: problem with binary file  
joe@invalid.address


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-13-05 10: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





[ Post a follow-up to this message ]



    Re: problem with binary file  
joe@invalid.address


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-13-05 10: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





[ Post a follow-up to this message ]



    Re: problem with binary file  
Måns Rullgård


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-14-05 01:48 AM

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





[ Post a follow-up to this message ]



    Re: problem with binary file  
Gianni Mariani


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-14-05 07: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;
}

}





[ Post a follow-up to this message ]



    Re: problem with binary file  
Maxim Yegorushkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-14-05 10: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.






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 02:35 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register