[go: up one dir, main page]

0% found this document useful (0 votes)
29 views3 pages

Exercise-5 3 1

The document outlines an exercise in C programming that requires writing a program to count characters, words, and lines in a text file, while also allowing the user to print and append text to the file. It details the functions needed for the program, including menu handling, counting lines, words, and characters, printing file contents, and appending strings. The document also specifies the naming convention for the source code file to be submitted.

Uploaded by

Reshma Wayadande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

Exercise-5 3 1

The document outlines an exercise in C programming that requires writing a program to count characters, words, and lines in a text file, while also allowing the user to print and append text to the file. It details the functions needed for the program, including menu handling, counting lines, words, and characters, printing file contents, and appending strings. The document also specifies the naming convention for the source code file to be submitted.

Uploaded by

Reshma Wayadande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

The Tech Series: C Language

Exercise 5.3.1

For this exercise, I will provide you with a text file (test.txt), and you will write a program that
can count the number of characters, words, and lines in the provided file. Your program should
be capable of also printing the contents of the file as well as appending new text to the end of
the file.

• Function 1: void menu (void);


o Ask the user for a file name
o Open the file with access mode append and read(a+) and check for success or
failure
o Create an infinite loop. Inside the loop:
1. call the menu function
2. ask the user to select an action from the menu (check for valid input)
3. call the appropriate function based on the user input
4. the program ends when the user selects (e) for Exit
o Please see the sample output on the next two pages
• Note that when the file has first opened, any operation will be done starting at the
beginning of that file. As the file is read the new position is retained.
• For this exercise, use the function rewind (at the end of each function that is modifying
or reading the file), to go back to the starting position
• Function 2: int lineCount (FILE *);
o Count non-empty lines. One way to do this is to loop with getc and to count the
newline ( ‘\n’ ) character when the line is not empty.
• Function 3: int wordCount (FILE *);
o Count the number of words in the file; a character is a word. One way to do this
is to loop with fscanf until you get to the end of file character (EOF).
• Function 4: int charCount (FILE *);
o Count the number of characters in the file. One way to do this is to loop with
getc but not counting white spaces ( ‘ ‘ ) and the newline ( ‘\n’ ) characters.
• Function 5: void printFile (FILE * fp);
o Print the contents of the file. One way to do this is to loop with getc capturing
and displaying everything until you get to the end of file character (EOF)
• Function 6: void addStringToFile (FILE * fp);
o Append a string to the end of the file. One way to do this is to use the function
gets to capture the user string, fputc to a new line character ( ‘\n’ ) and fputs to
add the string to the file
The Tech Series: C Language
The Tech Series: C Language

• Please save and submit your source code (.c file) solution.
• Solution file naming convention:
o First_name_Last_name_exercise_number.c
o Please use ( - ) to indicate the exercise number
o Example: Haidar_Alaubiydy-5-3-1.c

You might also like