I/O

This guide discusses the C functions used for reading and writing characters and strings, with examples of their usage.

Character Functions for Input

getc()Function:

  • Purpose: Reads a single character from a file.

  • Syntax: int getc(FILE *stream);

Examples

Reading from a file:

#include <stdio.h>
int main() {
    char ch = '\0';
    FILE *fp;
    if ((fp = fopen("test","r")) != NULL) {
        while ((ch = getc(fp)) != EOF) {
            printf("%c",ch);
        }
        fclose(fp);
    }
    return 0;
}

Reading from stdin (i.e., keyboard):

int main() {
    char ch = getc(stdin);
    printf(">>> %c\n", ch);
    return 0;
}

Alternative methods:

int main() {
    int ch;
    while ((ch = getchar()) != EOF) printf("%c\n", ch);
    return 0;
}

With spaces captured:

int main() {
    int ch;
    while (isspace(ch = (char)getchar()));
    printf("%c\n", ch);
    return 0;
}

ungetc()Function:

  • Purpose: Pushes a character back into the stream.

  • Syntax: int ungetc(int char, FILE *stream);

Example:

#include <stdio.h>
int main() {
    FILE *fp;
    int c;
    char buffer[256];
    fp = fopen("test", "r");
    while (!feof(fp)) {
        c = getc(fp);
        if (c == '!') ungetc('+', fp);
        else ungetc(c, fp);
        fgets(buffer, 255, fp);
        fputs(buffer, stdout);
    }
    return 0;
}

Character Functions for Output

putc()Function:

  • Purpose: Writes a single character to a file or stdout.

  • Syntax: int putc(int char, FILE *fp);

  • Usage: putc('\n', stdout);

Example

Redirecting input to a file:

int main() {
    int ch;
    while ((ch = getchar()) != EOF) putchar(ch);
    ungetc(ch, stdin);
    printf("EOF signal detected!\n");
    return 0;
}

Use the program like:

./main < infile

fputc()Function:

  • Purpose: Writes a character to the specified stream.

  • Syntax: int fputc(int character, FILE *stream);

Example

Writing characters a-z to a file:

int main() {
    FILE *pfile = NULL;
    char c;
    pfile = fopen("test", "w");
    for (c = 'A'; c <= 'Z'; c++) fputc(c, pfile);
    fclose(pfile);
    return 0;
}

String Functions for Input

getline()Function:

  • Purpose: Reads a line from the specified stream.

  • Syntax: ssize_t getline(char **buffer, size_t *size, FILE *stream);

Example

Reading a line with getline():

#include <stdio.h>
#include <stdlib.h>
int main() {
    char *buffer = NULL;
    size_t bufsize = 32;
    size_t characters;
    buffer = malloc(bufsize * sizeof(char));
    if (buffer == NULL) exit(1);
    printf("Type something: ");
    characters = getline(&buffer, &bufsize, stdin);
    printf("%zu characters were read.\n", characters);
    printf("You typed: %s\n", buffer);
    return 0;
}

fscanf()Function:

  • Purpose: Reads formatted input from a file.

  • Syntax: int fscanf(FILE *fp, const char *format, ...);

Example:

#include <stdio.h>
int main() {
    FILE *fp;
    char buff[255];
    fp = fopen("test", "r");
    while (fscanf(fp, "%s", buff) != EOF) {
        printf("%s ", buff);
    }
    fclose(fp);
    return 0;
}

Formatting Functions

sprintf()Function:

  • Purpose: Writes formatted output to a string.

  • Syntax: int sprintf(char *str, const char *format, ...);

Example:

#include <stdio.h>
int main() {
    char buffer[50];
    int a = 10, b = 20;
    sprintf(buffer, "%d plus %d is %d", a, b, a+b);
    printf("%s", buffer);
    return 0;
}

Last updated