Solved

Reading and writing binary files (for compression purposes)

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

776 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