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 7749399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:02:31+00:00 2026-06-01T11:02:31+00:00

I am trying to get a basic understanding on how to use fputc in

  • 0

I am trying to get a basic understanding on how to use fputc in C. I have read some documentation that is out there and believed I had it right. But every time I try to use the script I wrote by executing ./fputc > test.txt where text.txt is a text file with one line of text.

This is my script:

int
main(int argc, char **argv){
    int ch;
    FILE *input;
    input = fopen("text.txt", "w+");
    while ((ch = getchar()) != EOF){
        fputc(ch, input);
    }
    fclose(input);

    return 0;
}

I get no errors on compilation and for some reason the script does not reach EOF at the end of the text file. Shouldn’t the getchar return EOF when it reached the end of the text file?
The text (text.txt) file does not appear to be edited, although it is created. So somewhere in my while loop something is going wrong.

I am new to C programming (if you couldn’t tell) and this little script has me befuddled.

Any help would be appreciated, or any links to sites with further detail would also be great.

Cheers,

S.

  • 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-06-01T11:02:32+00:00Added an answer on June 1, 2026 at 11:02 am

    What you in essence say is:

    • Console: Run my_program and write anything it outputs to test.txt.

    • Program: Open text.txt and write any input to stdin to that file.


    Your console normally have three standard streams stdin, stdout and stderr. These streams you can redirect. If you are on Windows also look at i.e. redirection.

    When you say ./my_prog > test.txt, what you tell your console, (not my_prog), is to write anything my_prog writes to stdout to the file test.txt.

    If you in your code say i.e. printf("Hello");, then Hello would be written to the file test.txt.

    If you had turned your redirection around by saying ./my_prog < test.txt instead, would be; stream the file test.txt to my_prog. Which, in turn, if there was any text in test.txt would result in a copy of test.txt to text.txt.

    Now in your code you say:

    int main(void) 
    {
        int ch;
        FILE *input;
    
        /* Here you open a handle to the file text.txt for reading and writing */
        input = fopen("text.txt", "w+");
    
        while ((ch = getchar()) != EOF) { /* get next char from stdin */
            fputc(ch, input); /* write that char to the handle input */
        }
        fclose(input); /* close the handle */
    
        return 0;
    }
    

    So what happens, the way you run it, is:

    In your code:

    1. Open text.txt
    2. Wait for input (data entered to stdin) – typically user entering text to console, passed to program when Enter is pressed.

    In console:

    1. Redirect anything from my_prog to test.txt.

    You say:

    the script does not reach EOF

    Well, as it reads from stdin it will only (not without exception) get EOF under two conditions.

    1. If you redirect a file to your program. I.e. ./my_prog < foo.txt (notice <, not >).
            – What would happen then is that my_prog would read the data from the file foo.txt and when that file ends your program would receive a EOF. And, hence quit.

    2. If you manually enter EOF to stdin.
            – On Linux and OSX Ctrl–D, on Windows Ctrl–Z


    Now, if you test this by typing text to console remember that write actions like fputc()is buffered. What this mean is that the data is not written to the file right away, but only when a given amount of data is in buffer, fflush() is called, stream is closed, you turn off buffering, etc.

    Also; if you run your program. Enter text, enter some more text, and then hit Ctrl-C to abort the program it is a big chance you end with no data in your text.txt.

    The reason for this is that the program is killed and thereby fclose() never called, and hence no flush to file.


    On your further endeavors in programming it would be a very good idea to make a habit of not presuming anything. I.e. do not presume fopen() is OK.

    FILE *fh;
    char *outfile = "foo.txt";
    
    if ((fh = fopen(outfile, "w")) == NULL) {
        fprintf(stderr,
                "Unable to open file %s\n --",
                outfile);
        perror(" fopen() ");
        return 1;
    }
    

    Most functions has a way to check if operation was a success. I.e:

    if (fputc(ch, fh) != ch) { err ...
    

    This will make your code a lot safer, give you hints on where it fails etc.


    Some links:

    • Look at redirection links at top of post.
    • Look at the functions in stdio.h (good overview), stdio.h (examples etc.). I.e.:
      • stdin
      • stdout
      • stderr
      • fopen()
      • fflush()
      • setvbuf()
      • setbuf()
      • …
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to get some basic understanding of console functionalities. I am having issues so
I am new to jquery, with some basic understanding. I am trying to use
I have been trying to figure out how to use JAX-RS for quite some
I am trying to get my head round some basic erlang functionality and I
I have some small problem in understanding Value Objects in Flex... I'm trying to
I'm trying to get a basic understanding of floating point operations on x86. I
Mornin', I'm trying to just get basic encryption working using System.Security.Cryptography.RjindaelManaged. I have google
I cannot find an example on this. I'm trying to get a basic understanding
Trying to get a basic understanding of T-SQL here in SQL Server 2008. Suppose
I am trying to get some basic unit tests up and running on the

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.