Character Input/Output Functions in C

Character input functions read one character at a time from a text stream, while character output functions write one character at a time to a text stream.

Our first example program will read from standard input and then print out the data back to the screen.  The getchar() function reads the next character from stdin and returns its value. The putchar() function writes one character to standard output.

#include <stdio.h>

#define LENGTH 50

int main(void){
    
    
    //return type of getchar()
    //is int not char
    int character;
    int i = 0;
    char string[LENGTH];
    
    while(i<LENGTH-1){
        //EOF can be sent using CTRL-D on
        //Linux systems
        if((character=getchar())!=EOF){
            string[i++]=character;
        } else {
            break;
        }
    }
    
    string[LENGTH-1]='';
    
    i=0;
    //print out text
    while(string[i]!=''){
        putchar(string[i++]);
    }

    return 0;
}

Note that the return type of the getchar() function is an int and not a char; this is because EOF is defined as an integer.

The fputc() function writes the character argument to the specified file stream. The fist argument is the character to be written and the second argument is the file. If the character is written, the fputc() function returns the value of the character. In the event of an error, it returns EOF.

#include <stdio.h>


int main(void){
    

    FILE *filePointer;
    int c;
    
    printf("Input will be copied toa file.\n");
    printf("When finished, enter EOF.\n\n");
    
    
    if(!(filePointer=fopen("testfile.txt", "w")))
    {
        printf("Error opening the file for writing.\n");
        return 1;
    }
    
    while((c = getchar())!=EOF){
        fputc(c, filePointer);
    }
    
    if((fclose(filePointer))==EOF){
        printf("Error closing file.\n");
        return 100;
    }
    return 0;
}

The fclose() function closes the file stream. It takes the pointer to the file stream object as its argument, and returns 0 upon success, and EOF in the event of failure.

The fgetc() function reads the next character from the file stream and converts it to an int value. If it detects an end of file, it returns EOF. Note that an EOF is also returned if any errors happen.

Our next program will copy the contents of one text file to another. 

#include <stdio.h>


int main(void){
    
    int character;
    FILE *copyFromPtr;
    FILE *copyToPtr;
    
    printf("Copying file...\t");
    
    if(!(copyFromPtr = fopen("testfile.txt", "r"))){
        printf("Error opening testfile.txt for reading.\n");
        return 1;
    }
    
    if(!(copyToPtr = fopen("testfile2.txt", "w"))){
        printf("Error opening testfile2.txt for writing.\n");
        return 1;
    }
    
//read one character from one file
//and then copy to another
    while((character=fgetc(copyFromPtr))!=EOF){
        fputc(character, copyToPtr);
    }
    
    if((fclose(copyFromPtr))==EOF){
        printf("Error closing the file we were copying from.\n");
        return 1;
    }
    
    if((fclose(copyToPtr))==EOF){
        printf("Error closing the file we were copying to.\n");
        return 1;
    }
    
    printf("File successfully copied.\n");
    
    return 0;
}

Want to learn more? My book on C programming contains over 200 brand new example programs, exercises, and detailed explications. Take a look at: http://www.amazon.com/Big-Als-C-Standard-ebook/dp/B00A4JGE0M/

I also have a book on working from the Linux command line, only .99 cents for a limited time: http://www.amazon.com/Big-Als-Linux-Fedora-CLI-ebook/dp/B00GI25V30/

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s