read files in c

To read files in C, you can use the fopen() function to open a file and obtain a file pointer. Then, you can use the fscanf() or fgets() functions to read the contents of the file. Finally, you should close the file using the fclose() function. Here is an example:

#include <stdio.h>

int main() {
   FILE *file;
   char buffer[100];

   file = fopen("filename.txt", "r");
   if (file == NULL) {
      printf("File could not be opened.");
      return 1;
   }

   while (fgets(buffer, sizeof(buffer), file) != NULL) {
      printf("%s", buffer);
   }

   fclose(file);

   return 0;
}

This code opens a file called "filename.txt" in read mode, reads its contents line by line using fgets(), and prints each line to the console. Remember to replace "filename.txt" with the actual name of the file you want to read.