Reading files in C

Vishal Rashmika published on
1 min, 43 words

Categories: C

1.fopen

    FILE* fptr;
    //argv[1] = filename
    fptr = fopen(argv[1], "r");

    if (fptr == NULL) {
        printf("Error: Missing File \"%s\"\n",argv[1]);
        exit(0);
    }
    else{

        char ch;
        do {
            ch = fgetc(fptr);
            printf("%c", ch);
        } while (ch != EOF);
        // Closing the file
        fclose(fptr);
    }