Day 12 of Programming in C

Write a program to input alphabets in a text file.


#include <stdio.h>
int main(){
    FILE *fptr = fopen("abc.txt","w");
    if (fptr == NULL) {
        printf("Error opening the file.\n");
        return 1;
    }
    for(int i=65;i<=90;i++){
        putc(i,fptr);
    }
    fclose(fptr);
    return 0;
}

    

Write a program that copies text from one file and writes into another.


#include <stdio.h>

int main() {
    FILE *source, *destination;
    char ch;
    source = fopen("source.txt", "r");
    if (source == NULL) {
        printf("Error opening source file.\n");
        return 1;
    }
    destination = fopen("destination.txt", "w");
    if (destination == NULL) {
        printf("Error opening destination file.\n");
        fclose(source);
        return 1;
    }
    while ((ch = fgetc(source)) != EOF) {
        fputc(ch, destination);
    }
    fclose(source);
    fclose(destination);
    return 0;
}


        
    
    

Comments

Popular Posts