Skip to content

5.1 Input / Output

In C programming, Input/Output (I/O) refers to the mechanism through which the program interacts with the outside world—either reading data from input devices (keyboard, file, etc.) or writing data to output devices (screen, file, etc.).

There are two main types of I/O in C:

  1. Standard Input/Output (Keyboard and Screen)
  2. File Input/Output (Reading and Writing from/to files)

1. Standard Input/Output

The C standard library provides functions to handle input and output from the console (keyboard and screen). These are defined in the stdio.h header file.

Important Functions
  • printf(): Used to output data to the screen.

    printf("Hello, World!\n");
  • scanf(): Used to read input from the user (keyboard).

    int x;
    scanf("%d", &x); // Reads an integer from the user
  • getchar(): Reads a single character from the standard input (keyboard).

    char c = getchar(); // Reads a character
  • putchar(): Outputs a single character to the standard output (screen).

    putchar('A'); // Outputs the character 'A'
  • fgets(): Reads a string from the input (with a buffer size limit).

    char buffer[100];
    fgets(buffer, sizeof(buffer), stdin);
  • puts(): Outputs a string to the standard output.

    puts("Hello, World!"); // Prints the string and adds a newline

2. File Input/Output (File I/O)

In C, file I/O is used to read from and write to files. These operations are performed using file pointers and functions defined in stdio.h. Before working with files, you need to open them using the fopen() function, and after you’re done, you should close the file using fclose().

Opening and Closing Files:
  • fopen(): Opens a file.

    FILE *file = fopen("example.txt", "r"); // Opens file for reading

    The fopen() function takes two arguments:

    • filename: The path to the file you want to open.
    • mode: A string that specifies the operation mode (e.g., reading, writing, appending).
      • "r": Read (file must exist).
      • "w": Write (create or truncate file).
      • "a": Append (create or open for appending).
      • "rb", "wb", etc.: Open files in binary mode.
  • fclose(): Closes a file that has been opened.

    fclose(file);
Reading from Files
  • fgetc(): Reads a single character from the file.

    char c = fgetc(file); // Reads a character from the file
  • fgets(): Reads a line (string) from the file.

    char buffer[100];
    fgets(buffer, sizeof(buffer), file); // Reads a line from the file
  • fread(): Reads a block of data from a file into memory.

    size_t result = fread(buffer, sizeof(char), sizeof(buffer), file);
Writing to Files
  • fputc(): Writes a single character to the file.

    fputc('A', file); // Writes a character to the file
  • fputs(): Writes a string to the file.

    fputs("Hello, File!", file);
  • fwrite(): Writes a block of data to a file.

    size_t result = fwrite(data, sizeof(char), size, file);
Error Handling
  • feof(): Checks if the end of the file has been reached.

    if (feof(file)) {
    printf("End of file reached\n");
    }
  • ferror(): Checks if there was an error with the file stream.

    if (ferror(file)) {
    printf("Error reading from file\n");
    }
Example of File I/O
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w"); // Open file for writing
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
// Writing to the file
fprintf(file, "Hello, World!\n");
fputs("This is a file I/O example.\n", file);
fclose(file); // Close the file after writing
// Reading from the file
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
char buffer[100];
while (fgets(buffer, sizeof(buffer), file)) {
printf("%s", buffer); // Print each line from the file
}
fclose(file); // Close the file after reading
return 0;
}