Read File Function
size_t fread(void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);
Write File Function
size_t fwrite(const void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);
Example:
Read entire file
/* Example: fgets in C language */ #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include <stdbool.h> #include <errno.h> #include <unistd.h> int main(){ FILE *in=fopen("test.txt","r"); char c; while((c=fgetc(in))!=EOF) putchar(c); fclose(in); return 0; }
Read the file line by line, and each line is readed up to 100 characters
/* Example: Read file line by line in C language */ #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include <stdbool.h> #include <errno.h> #include <unistd.h> int main(){ FILE *fp=fopen("test.txt","r"); char string[100]; while(fgets(string, 100, fp)) { printf("%s\n", string); } return 0; }