Solved

Reading and writing binary files (for compression purposes)

Posted on 2002-06-11
3
271 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
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

896 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now