#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);

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter8: I/o Streams And Data Files
Section: Chapter Questions
Problem 8PP: (Data processing) A bank’s customer records are to be stored in a file and read into a set of arrays...
icon
Related questions
Question

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.

Q#include <stdio.h>
#include <string.h>
1
3
4
#define MAX 4000
eint main(void)
{
FILE* bkPtr;
7
8.
char buffer[MAX];
10
fopen_s(&bkPtr, "defoe-robinson-103.txt", "r");
if (bkPtr
puts("Unable to open file.");
}
else {
11
12
NULL) {
==
13
14
15
while (fgets(buffer, MAX, bkPtr)) {
puts(buffer);
}
16
17
18
19
20
fclose(bkPtr);
21
22
Transcribed Image Text:Q#include <stdio.h> #include <string.h> 1 3 4 #define MAX 4000 eint main(void) { FILE* bkPtr; 7 8. char buffer[MAX]; 10 fopen_s(&bkPtr, "defoe-robinson-103.txt", "r"); if (bkPtr puts("Unable to open file."); } else { 11 12 NULL) { == 13 14 15 while (fgets(buffer, MAX, bkPtr)) { puts(buffer); } 16 17 18 19 20 fclose(bkPtr); 21 22
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr