Opens a file and prints A through Z into the file - C File

C examples for File:File Operation

Description

Opens a file and prints A through Z into the file

Demo Code

#include <stdio.h>
#include <stdlib.h>

FILE * fptr;//from   ww  w  .j a v a2 s  .  com

int main()
{
    char letter;
    int i;
    fptr = fopen("C:\\test\\letters.txt", "w+");
    
    if (fptr == 0){
        printf("There was an error while opening the file.\n");
        exit(1);
    }

    for (letter = 'A'; letter <= 'Z'; letter++){
        fputc(letter, fptr);
    }
    // Now read the file backwards
    fseek(fptr, -1, SEEK_END); // Minus 1 byte from the end
    printf("Here is the file backwards:\n");
    for (i = 26; i > 0; i--)
    {
        letter = fgetc(fptr);
        // Reads a letter, then backs up 2
        fseek(fptr, -2, SEEK_CUR);
        printf("The next letter is %c.\n", letter);
    }

    fclose(fptr);

    return(0);
}

Result


Related Tutorials