#include #include #define MAX 4000 int main(void) FILE* bkPtr; char buffer[MAX]; fopen_s(&bkPtr, "defoe-robinson-103.txt", "r"); if (bkPtr puts("Unable to open file."); } else { while (fgets(buffer, MAX, bkPtr)) { puts(buffer); } NULL) { == fclose(bkPtr);
Hello,
How can I store a whole book in a file in c language?
I had some issues because the string is too long.
here is the question:
For this assignment I have uploaded a large text file that contains the entire book “Robinson Crusoe” by
William Defoe as file defoe-robinson-103.txt
Consider the code shown below. It uses the fgets function.
fgets is used to read (“get”) strings (“s”) from a file (“f”), hence the name “fgets”. It takes the
following arguments:
1. A pointer to an array large enough to store the string. In our case that is the char array called
buffer of size MAX.
2. The size of the array, in our case the symbolic constant MAX, set equal to 4000 in line 4.
3. The file pointer of the file to read from, in our case bkPtr.
Each call to fgets returns a string until the newline (“\n”) character or EOF is encountered. The code
reads strings from the file defoe-robinson-103.txt in the while loop and prints them to screen.
Implement this program and run it.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps