Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7061279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:27:16+00:00 2026-05-28T04:27:16+00:00

Hello i had a simple copy file program in C but i cant explain

  • 0

Hello i had a simple copy file program in C but i cant explain why i get different output in the destination file when i use the 2nd method.
The correct output with for loop:

I am the worst programmer in the world!
:D
 And this is bla bla bla bla
 more bla bla bla...

BUT with while loop a random char is generated in EOF:

I am the worst programmer in the world!
:D
 And this is bla bla bla bla
 more bla bla bla...


The code is

int main()
{
int i;
char ch;
create_files();
FILE *src = fopen("best.txt", "r");
FILE *dst = fopen("copied.txt", "w");
for(i=getc(src); i!=EOF; i=getc(src))  //correct copy
    {
        putc(i, dst);
    }

/* while(!feof(src))                  //woot?
    {
        ch=fgetc(src);
        fputc(ch,dst);
    }*/

fclose(dst);
fclose(src);
return 0;
}

void create_files()
{
    FILE *fp;
    fp = fopen("best.txt","w");
    fprintf(fp,"I am the worst programmer in the world!\n:D\n And this is bla bla bla bla\n more bla bla bla...\n");
    fclose(fp);
}

i ve used both fputc or putc and fgetc or getc and still the same. Did i forget something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:27:17+00:00Added an answer on May 28, 2026 at 4:27 am

    What

    while (!feof(src)) {
        ch=fgetc(src);
        fputc(ch,dst);
    }
    

    does, is:

    1. check for EOF
    2. read a character, possibly causing EOF
    3. output the character just read, without checking for EOF.

    When EOF occurs, (3) is still executed before the check in (1) in the next iteration. The special value EOF is converted to a char and output.

    The correct loop is

    while ((ch = fgetc(src)) != EOF)
        fputc(ch, dst);
    

    assuming you give ch the type int, because a char cannot represent EOF. Note the assignment within the check; some programmers will tell you that’s ugly, but so many use it that you might as well get used to it. Your variant for loop is correct as well.

    (Aside 1: fputc is equivalent to putc and fgetc to getc, as long as you don’t try to use them in a function pointer context.)

    (Aside 2: your while loop also doesn’t check for stream errors, while checking for EOF returns catches that as well.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I am compiling a program with make but I get the error of
First, apologies,this should be simple but I've had too much coffee and cannot wrap
Had a quick question.. Googled but nothing worthwhile found.. I have a simple type
i had made a program for text to speech convertion in python..and now want
I am trying to design some simple GUIs with Glade to use with Ruby.
I've had Jetty recommended as a good container for fast and simple development, in
I want to do something really simple like this: JPanel htmlPanel = new HtmlPanel(<html><body><h1>hello
I thought I had custom events nailed in Flex, but seemingly not. I am
Hello I'm developing a simple application where the user can change a content in
Question Hello All, I've had some confusion for quite some time with essentially flooring

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.