How to read from a file in C Programming

#include <stdio.h>

int main(void)
{
    char text[100];
    FILE *fp = fopen("readme.txt", "r");
    if (!fp)
    {
        printf("Cannot open the file.\n");
        return 1;
    }
    while (fgets(text, 100, fp) != NULL)
    {
        printf("%s", text);
    }
    fclose(fp);

    return 0;
}

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *