write a program names2.c which opens this same file, reads the words in the file one by one and and prints these words so that the screen output looks identical to the content of names.txt (and the display above). One way to detect the end of a line (for this text) is to check the last character of each word for a period and add a newline after each period found.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Hello, I'm having problem with step 2. Step 1 is correct (name.c). I need help with name2.c (my code and question is attached.

#include <stdio.h>

int main(void) {

    FILE *file_p;

    file_p = fopen("names.txt", "w");

    char *text = "Alice talks to Bob.\n";

    fputs(text, file_p);

    text = "Bob talks to Alice.\n";

    fputs(text, file_p);

    text = "Eve listens in the middle.\n";

    fprintf(file_p, "%s", text);

    fclose(file_p);

return 0;

}

Then write a program names2.c which opens this same file, reads the words in the file one by one and and prints these words so that the screen output looks identical to the content of names.txt (and the display above). One way to detect the end of a line (for this text) is to check the last character of each word for a period and add a newline after each period found. (The getline function solves this problem for a general text file.)

Finally, write a program names3.c which just prints out the last line in the file without assuming that there are just three lines. Do this by setting the file- cursor to the last character of the file with fseek, then searching backwards a character at a time with fscanf with format string "%c" while looking for a newline character (ignoring that at the end of the file). Then use getline to read that last line.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

stating 'segmentation fault (core dump). Also, directions state to use getline?

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Constants and Variables
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education