Advertisement

06.16.2004 at 02:38AM PDT, ID: 21027080
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

How to read a binary file in C
Tags: binary, c, file, read

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

Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: sudhakar_koundinya
Solution Provided By: sunnycoder
Participating Experts: 5
Solution Grade: A
Views: 559
Translate:
Loading Advertisement...
06.16.2004 at 02:39AM PDT, ID: 11322723

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 02:40AM PDT, ID: 11322733

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 02:43AM PDT, ID: 11322746

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 02:58AM PDT, ID: 11322790

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 03:01AM PDT, ID: 11322810

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 03:03AM PDT, ID: 11322815

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 03:07AM PDT, ID: 11322832

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 03:09AM PDT, ID: 11322849

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 03:16AM PDT, ID: 11322871

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 03:31AM PDT, ID: 11322926

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 03:42AM PDT, ID: 11322969

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 03:46AM PDT, ID: 11322988

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 03:46AM PDT, ID: 11322990

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 03:47AM PDT, ID: 11322995

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 03:52AM PDT, ID: 11323024

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 03:54AM PDT, ID: 11323033

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 04:21AM PDT, ID: 11323161

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 04:35AM PDT, ID: 11323243

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 04:37AM PDT, ID: 11323250

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 04:42AM PDT, ID: 11323278

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 04:42AM PDT, ID: 11323282

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 04:45AM PDT, ID: 11323292

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 04:45AM PDT, ID: 11323295

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 04:46AM PDT, ID: 11323304

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 04:48AM PDT, ID: 11323317

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 04:51AM PDT, ID: 11323345

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 04:54AM PDT, ID: 11323364

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 04:56AM PDT, ID: 11323375

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 05:07AM PDT, ID: 11323461

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 05:19AM PDT, ID: 11323565

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 05:23AM PDT, ID: 11323592

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 07:11AM PDT, ID: 11324513

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2004 at 07:12AM PDT, ID: 11324524

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.28.2004 at 03:22AM PDT, ID: 11414263

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.07.2004 at 06:26PM PDT, ID: 11497970

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
06.16.2004 at 02:39AM PDT, ID: 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
 
06.16.2004 at 02:40AM PDT, ID: 11322733
Hi sudhakar_koundinya,

This link has a working example

http://www.cplusplus.com/ref/cstdio/fread.html

Cheers!
Assisted Solution
 
06.16.2004 at 02:43AM PDT, ID: 11322746

Rank: Sage

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(buffer),in)) > 0)
{
   fwrite(buffer,1,len,out);
}

fclose(in);
fclose(out);

of course you need better error handling

check these links

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20587923.html
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_10081782.html


Sunnycoder
Accepted Solution
 
06.16.2004 at 02:58AM PDT, ID: 11322790

Rank: Sage

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
 
06.16.2004 at 03:01AM PDT, ID: 11322810
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(unsigned char), 1,in);
             int got=fread(b, 1,sizeof(b),in);
             return got;
                  

}



//implemetation methods

HeaderBlockReader::HeaderBlockReader(char* file)
{
      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();
}
 
06.16.2004 at 03:03AM PDT, ID: 11322815

Rank: Sage

this is C++ ... I would suggest sticking to either C or C++ ... you are mixing the C++ code with C programming style
 
06.16.2004 at 03:07AM PDT, ID: 11322832
Yes I do agrre,

When I worked on this previously, i never faced the problems.

Now this is immediate requirement for me. to integrate c,c++ and java files and strucking in file reading concepts :(
 
06.16.2004 at 03:09AM PDT, ID: 11322849

Rank: Sage

check http:#11322790  for reason why your code was not working and http:#11322746 for a working example
 
06.16.2004 at 03:16AM PDT, ID: 11322871
>>   int got=fread(b, 1,sizeof(b),in);

but i am doing the same know? Then what is the reason for not working
 
06.16.2004 at 03:31AM PDT, ID: 11322926

Rank: Sage

>Then what is the reason for not working

What is the output you see ?
 
06.16.2004 at 03:42AM PDT, ID: 11322969
instead of reading all 512 bytes it is just reading 6 bytes
 
06.16.2004 at 03:46AM PDT, ID: 11322988
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","w");

      int len;
      char buffer[1024];

      while((len = fread(buffer,1,sizeof(buffer),in)) > 0)
      {
         fwrite(buffer,1,len,out);
      }

      fclose(in);
      fclose(out);
}
 
06.16.2004 at 03:46AM PDT, ID: 11322990

Rank: Sage

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
 
06.16.2004 at 03:47AM PDT, ID: 11322995

Rank: Sage

>I just done a test run on document of size 19kb. but the out put i got is of 20 kb
try comparing the files ... Are you sure you are not seeing size on disk for doc2 ... Also, which platform and which compiler ?
 
06.16.2004 at 03:52AM PDT, ID: 11323024
>>buffer[i] = '\0';       <<<<<<<<<<<, not a good idea ...

after removing that code only i am getting this error

I am compiling the program on v