I'm simply trying to print out the contents of a text file to make sure I am in fact opening and reading a text file correctly. I also want to store all the characters in an array. Here's my code so far:
I tried fgets() to store the characters of the text file in the contents[] array.Code:#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <fcntl.h> #include <sys/stat.h> int main(int argc, char** argv) { FILE * textfile; int max_chars = 1000; char contents[max_chars]; textfile = fopen("file.txt", "r"); fgets(contents,max_chars,textfile); fputs("%s",contents); return (EXIT_SUCCESS); }
Then I tried fputs() to print out the text file.
Am I using the right approach using fgets() and fputs() or should I be doing something else?



2Likes
LinkBack URL
About LinkBacks



