0

I made simple program that can copy source file and paste another defined file.

Below is my code.

int main(void) {
        FILE *fp_src;
        FILE *fp_dest;

        char src[100];
        char dest[100];

        fputs("source file : ", stdout);
        gets_s(src);
        fp_src = fopen(src, "rb");

        fputs("dest file : ", stdout);
        gets_s(dest);
        fp_dest = fopen(dest, "wb");


        char ch;
        //int ch;
        while ((ch = fgetc(fp_src)) != EOF) {
            fputc(ch, fp_dest);
        }

        fclose(fp_src);
        fclose(fp_dest);
        return 0;
    }

When I use fgetc function, it returns integer-type value because the EOF needs 4-bytes space. I know this concept, but, when I was making this program, I used the character-type for storing the returning value by mistake. And then, I ran this program, the destination file (the file that I want to copy) cannot properly create. (the file size is 0-byte) Therefore I changed the character-type into integer-type and then I found it was working fine.

As I mentioned above, I know that fgetc returns the integer-type value. However, I don't think that using character-type for fgetc wouldn't be a problem but it was a critical problem when copying file with fgetc function.

So I didn't get it why this situation happened.

Could you help me understand it more detailed?

2
  • MSDN confirms that gets_s() needs a length argument too. Did the code compile without warnings? If so, you need to turn up the warning levels on your compiler — or get a better compiler. Commented Aug 8, 2016 at 4:56
  • You don't verify that the calls to gets_s() work; you don't verify that the calls to fopen() work. You don't know that the code should be working. And you need to use at minimum short, and may as well use int like everyone else does, to capture the return value from fgetc(). Anything else is error prone at best. Commented Aug 8, 2016 at 5:04

2 Answers 2

3

The use of the getchar-like functions are required to be able to return every possible character PLUS an end-file indication.

"Every possible character" is covered by the char type but you have to use a wider type to cover the "PLUS" bit.

In other words, your contention that "I don't think that using character-type for fgetc wouldn't (sic, assuming you meant to type 'would' here) be a problem" is dead wrong.

The prototype for fgetc() states quite clearly: int fgetc ( FILE * stream ); and that's what you should generally be using for the return value, an int. Anything else is asking for trouble (though, provided you understand the implicit conversions, you could also use wider or even thinner types but int is probably the best choice).

And, as an aside, you should generally check the return values from every call where failure can propagate problems (such as gets_s or fopen). And ensure you pass the correct number and/or type of parameters, something a decent compiler should warn you about.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. char cannot recover the EOF and getchar-like functions need to more space in order to cover plus bits. However, before end of the file, char-type can read and write the file.However, my program didnt' work when they read file for the first time.
1

It's worth noting that gets_s() takes two arguments, the char * to read into, and an rsize_t, the maximum number of chars to read - http://en.cppreference.com/w/c/io/gets

However, I think the root of the problem is that type char cannot represent EOF. I'd recommend using type int instead of char for your ch character, since fgetc() returns and int and fputc() takes an int as an argument.

1 Comment

uint8_t is no better than char (or unsigned char, or signed char); you must use at least a short and may as well use int.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.