Solved

Reading and writing binary files (for compression purposes)

Posted on 2002-06-11
3
280 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops 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.

628 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