Solved

Reading and writing binary files (for compression purposes)

Posted on 2002-06-11
3
269 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
Comment Utility
#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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.

743 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

17 Experts available now in Live!

Get 1:1 Help Now