Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading and writing binary files (for compression purposes)

Posted on 2002-06-11
3
Medium Priority
?
282 Views
Last Modified: 2010-04-15
Evening

Just to kill some time, I thought I'd write a compression algorithm. I didn't get far before I hit a problem. I got so far as reading in a binary file (notepad.exe) and trying to output it but when I tried the following code, the output was one byte larger than the input. Any ideas what is going wrong (the output executable seems to work but clearly isn't the same as the original).

Having looked at some compression code, there seems to be a lot of mucking around with bits. I am I supposed to do something funky with the bits before I output each character?

#include <stdio.h>

int main(void)
{
     FILE *in,*out;

     char i;

     in = fopen("c:\\tmp\\notepad.exe","rb");
     out = fopen("c:\\notepad.exe","wb");

     while (!feof(in))
     {
          i = getc(in);
          putc(i,out);
     }

     fclose(in);
     fclose(out);

     return 0;
}

rd707
0
Comment
Question by:rd707
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 200 total points
ID: 7071410
#include <stdio.h>

int main(void)
{
    FILE *in,*out;

    int i;

    in = fopen("c:\\tmp\\notepad.exe","rb");
    out = fopen("c:\\notepad.exe","wb");

    while( (i = getc(in)) != EOF )
    {
         putc(i,out);
    }

    fclose(in);
    fclose(out);

    return 0;
}
0
 
LVL 1

Expert Comment

by:tapasmondal
ID: 7071867
Hi rd707
there is a silly mistake. the prototype of putc function is as following:

int putc( int c, FILE *stream );

so if u defined as int instead of char, i think ur problem will be solved.

#include <stdio.h>

int main(void)
{
    FILE *in,*out;

    int i;

    in = fopen("c:\\tmp\\notepad.exe","rb");
    out = fopen("c:\\notepad.exe","wb");

    while (!feof(in))
    {
         i = getc(in);
         putc(i,out);
    }

    fclose(in);
    fclose(out);

    return 0;
}




tapas
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 7072159
The reason for this is that feof() does not return TRUE until AFTER a read operation has attempted to read beyond the end of the file. Therefore, after you've read the last character, your getc() call will return EOF, which you'll then write to the output file--THEN the feof() will break you out of your while() loop.

You should follow ozo's approach of using the return from getc() to see if you've reached the end of the file.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question