oadams
oadams

Reputation: 3087

Reading general file

I'm making a program that reads in a file from stdin, does something to it and sends it to stdout.

As it stands, I have a line in my program:

while((c = getchar()) != EOF){

where c is an int.

However the problem is I want to use this program on ELF executables. And it appears that there must be the byte that represents EOF for ascii files inside the executable, which results in it being truncated (correct me if I'm wrong here - this is just my hypothesis).

What is an effective general way to go about doing this? I could dig up documents on the ELF format and then just check for whatever comes at the end. That would be useful, but I think it would be better if I could still apply this program to any kind of file.

Upvotes: 3

Views: 150

Answers (4)

pmg
pmg

Reputation: 108968

You are doing it correctly.

EOF is not a character. There is no way c will have EOF to represent any byte in the stream. If / when c indeed contains EOF, that particular value did not originate from the file itself, but from the underlying library / OS. EOF is a signal that something went wrong.

Make sure c is an int though

Oh ... and you might want to read from a stream under your control. In the absence of code to do otherwise, stdin is subject to "text translation" which might not be desirable when reading binary data.

FILE *mystream = fopen(filename, "rb");
if (mystream) {
    /* use fgetc() instead of getchar() */
    while((c = fgetc(mystream)) != EOF) {
        /* ... */
    }
    fclose(mystream);
} else {
    /* error */
}

Upvotes: 1

Mel
Mel

Reputation: 6157

From the getchar(3) man page:

Character values are returned as an unsigned char converted to an int.

This means, a character value read via getchar, can never be equal to an signed integer of -1. This little program explains it:

int main(void)
{
        int a;
        unsigned char c = EOF;

        a = (int)c;
        //output: 000000ff - 000000ff - ffffffff
        printf("%08x - %08x - %08x\n", a, c, -1);
        return 0;
}

Upvotes: 0

NPE
NPE

Reputation: 500197

You'll be fine - the EOF constant doesn't contain a valid ASCII value (it's typically -1).

For example, below is an excerpt from stdio.h on my system:

/* End of file character.
   Some things throughout the library rely on this being -1.  */
#ifndef EOF
# define EOF (-1)
#endif

Upvotes: 3

ColWhi
ColWhi

Reputation: 1077

You might want to go a bit lower level and use the system functions like open(), close() and read(), this way you can do what you like with the input as it will get stored in your own buffer.

Upvotes: 1

Related Questions