LANGUAGE » C » LIBRARIES

Stdio

Basic input & output

PurposeFunctionDescription
Inputscanf()Read any kind of variable, syntax is similar to printf. Reading strings is tricky.
Inputfgets()Read strings. New line included.
Outputprintf()Output to stdout in the format specified.
Outputputs()Display string + a new line.
c
#include <stdio.h>

int main(int argc, char *args[]) {
    char name[20];
    int age;
    printf("Your name: ");
    fgets(name, 20, stdin);
    printf("Your age: ");
    scanf("%d", &age);
    printf("Hello %s, you are %d years old.\n", name, age);
    return 0;
}

File manipulation

Functions to manipulate content of files:

File operationFunction
Openfopen("path/file", "mode")
Seekfseek(FILE*, offset, SEEK_SET | SEEK_END | SEEK_CUR)
Closefclose(FILE*)
File typeRead functionWrite function
Textfscanf(FILE*, ...)fprintf(FILE*, ...)
Binaryfread(data*, size, times, FILE*)fwrite(data*, size, times, FILE*)

When opening a file, there are 3 basic open modes:

ModeUseCreate fileKeep contents
r rbReading
w wbWriting
a abAppending

You can also append a + (ex: wb+) so both read and write are available. The behavior of "Create file" and "Keep contents" is kept.

Example of reading and writing to text file.

c
#include <stdio.h>

int main() {
    char filename[] = "salary.txt";
    int salary;
    FILE *file = fopen(filename, "r");
    if (file) {
        fscanf(file, "%d", &salary);
        printf("Your salary was $%d per month.\n", salary);
        fclose(file);
    }
    printf("Your new salary: ");
    scanf("%d", &salary);
    printf("I'll remember your new salary of $%d\nSee you later!", salary);
    file = fopen(filename, "w");
    fprintf(file, "%d", salary);
    fclose(file);
    return 0;
}

Example of reading and writing to binary file.

c
#include <stdio.h>
#include <string.h>

int main() {
    char filename[] = "username.save";
    char user[50];
    FILE *file = fopen(filename, "rb");
    if (file) {
        fread(user, sizeof(user), 1, file);
        printf("Welcome back %s", user);
        fclose(file);
    }
    printf("Your new user name: ");
    fgets(user, 50, stdin);
    printf("I'll remember your user name, %sSee you later!", user);
    file = fopen(filename, "wb");
    fwrite(user, strlen(user), 1, file);
    fclose(file);
    return 0;
}