Hi sudhakar_koundinya,
This link has a working example
http://www.cplusplus.com/r
Cheers!
Main Topics
Browse All Topics
As I am working in Java since 5 years, I started forgetting the concepts of C :(.
I am not able to read the binary files such as Word Documents and PDFs into my buffer .
I used fopen and fread
I used open and read.
in both the options I have failed.
Please post me one working example
Thanks
Sudhakar
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi sudhakar_koundinya,
This link has a working example
http://www.cplusplus.com/r
Cheers!
Hi sudhakar_koundinya,
> ch = fgetc( stream );
> for( i=0; (i < 513 ) ; i++ )
> {
> buffer[i] = (char)ch;
> ch = fgetc( stream );
> printf("\n%d %d",i,ch);
> }
use fread instead of fgetc or fgets
FILE *in = fopen("in_file.txt","r");
FILE *out = fopen("out_file.txt","w");
in len;
char buffer[1024];
while((len = fread(buffer,1,sizeof(buff
{
fwrite(buffer,1,len,out);
}
fclose(in);
fclose(out);
of course you need better error handling
check these links
http://www.experts-exchang
http://www.experts-exchang
Sunnycoder
Hi Sudhakar,
FILE *stream;
char buffer[513];
int i, ch;
/* Open file to read line from: */
if( (stream = fopen( "c:\\a.doc", "rb" )) == NULL )
exit( 0 );
/* Read in first 80 characters and place them in "buffer": */ <<<<< 512 bytes
ch = fgetc( stream );
for( i=0; (i < 513 ) ; i++ )
{
buffer[i] = (char)ch;
ch = fgetc( stream );
printf("\n%d %d",i,ch); <<<<<< you will get equivalent int printed here ... good enough ... Note that first character read will not be printed.
}
/* Add null to end string */
buffer[i] = '\0'; <<<<<<<<<<<, not a good idea ... 0 and other such characters and character sequences can occur anywhere within binary data ... Depending on a terminator character for binary data is definitely bad idea ... You will be better off with the sound knowledge of the number of chracters you have read
printf( "%s\n", buffer ); >>>>> Since the file is binary, most likely you will get gibbersih which will terminate as soon as 0 occurs in the buffer ... That is the way strings (%s) behaves .... remember that this is not a string but binary data ... If you wish tp print it, use fwrite instead ... Something like
fwrite ( buffer, 1, 513, stdout);
stdout, is defined in stdio.h
fclose( stream );
cheers
sunnycoder
Hello All,
Thanks for the immediate responses, but the problem is still continuing
//Reading methods
int IOUtils::readFully(FILE* in,unsigned char b[],int length )
{
return readFully(in,b,0,length);
return 0;
}
int IOUtils::readFully(FILE* in, unsigned char b[] , int off, int len)
{
//int got=fread(b,sizeof(unsigne
int got=fread(b, 1,sizeof(b),in);
return got;
}
//implemetation methods
HeaderBlockReader::HeaderB
{
FILE* fp=fopen(file,"rb");
//int fp=_open(file,O_RDONLY);
if(fp>0)
{
unsigned char* _data=new unsigned char[512];
_data = new unsigned char[512];
IOUtils *utils=new IOUtils();
int i = utils->readFully(fp, _data,512);
printf("%d",i);
if(i!=512)
{
printf(" nota microsoft document");
}
else
{
LongField *longfield = new LongField(0, _data);
printf(" Possible it could be a Microsoft Document %x",longfield->get());
}
}
else
{
printf(" Document does not exists");
}
fcloseall();
}
check http:#11322790 for reason why your code was not working and http:#11322746 for a working example
As you said, intgrating c and c++ may gives problems
I just done a test run on document of size 19kb. but the out put i got is of 20 kb
here is the test program
#include <io.h>
#include <stdio.h>
void main()
{
FILE *in = fopen("c:\\a.doc","rb");
FILE *out = fopen("c:\\out_file.doc","
int len;
char buffer[1024];
while((len = fread(buffer,1,sizeof(buff
{
fwrite(buffer,1,len,out);
}
fclose(in);
fclose(out);
}
Nope, it is just printing 6 bytes ... see my comment http:#11322790
/* Add null to end string */
buffer[i] = '\0'; <<<<<<<<<<<, not a good idea ... 0 and other such characters and character sequences can occur anywhere within binary data ... Depending on a terminator character for binary data is definitely bad idea ... You will be better off with the sound knowledge of the number of chracters you have read
printf( "%s\n", buffer ); >>>>> Since the file is binary, most likely you will get gibbersih which will terminate as soon as 0 occurs in the buffer ... That is the way strings (%s) behaves .... remember that this is not a string but binary data ... If you wish tp print it, use fwrite instead ... Something like
fwrite ( buffer, 1, 513, stdout);
stdout, is defined in stdio.h
Also,
in your fwrite(),the 2nd and 3rd arguments should be interchanged.
See:
http://bama.ua.edu/cgi-bin
the 2nd arg. is the size of the buffer and the 3rd arg. is the number of items to be written.
Ankur,
>the 2nd arg. is the size of the buffer and the 3rd arg. is the number of items to be written.
from the man page
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE
*stream);
DESCRIPTION
The function fread reads nmemb elements of data, **each size bytes long**,
from the stream pointed to by stream, storing them at the location
given by ptr.
>This way you'll be writig len number of bytes in one go,while in the former case,you'll be writing one byte at a time,repeating
>this len times.
Memory locations in either case are recontiguous and are handled in the same way ... You wont be performing mulitple writes with the former
It says, write n items of size 1 byte starting from location x and not write 1 byte n times starting from location x ;o)
Hello you can try like this
int main () {
FILE * pFile;
long lSize;
char * buffer;
pFile = fopen ( "myfile.txt" , "rb" );
if (pFile==NULL) exit (1);
// obtain file size.
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file.
buffer = (char*) malloc (lSize);
if (buffer == NULL) exit (2);
// copy the file into the buffer.
fread (buffer,1,lSize,pFile);
/*** the whole file is loaded in the buffer. ***/
// terminate
fclose (pFile);
free (buffer);
return 0;
}
Business Accounts
Answer for Membership
by: sudhakar_koundinyaPosted on 2004-06-16 at 02:39:01ID: 11322723
Here is the example
FILE *stream;
char buffer[513];
int i, ch;
/* Open file to read line from: */
if( (stream = fopen( "c:\\a.doc", "rb" )) == NULL )
exit( 0 );
/* Read in first 80 characters and place them in "buffer": */
ch = fgetc( stream );
for( i=0; (i < 513 ) ; i++ )
{
buffer[i] = (char)ch;
ch = fgetc( stream );
printf("\n%d %d",i,ch);
}
/* Add null to end string */
buffer[i] = '\0';
printf( "%s\n", buffer );
fclose( stream );
I have tried, but not working for binary files