Computer science

computer science


Reading Files using Loops and Functions
Task 1
Task 2

  1. Open the filename using an ifstream and check to make sure it worked.
  2. Create a new file to save the results in using an ofstream. The file should be named based on the letter given. For example, if I callstart_with_letter("example.txt",'A')then the output file should be results_A.txt.
  3. Read each word in the file. If it starts with the upper or lower case version of the letter given as input, write the word to your output file followed by a newline and increase your count.
  4. Close both files and return the count.

Overview: We will take a book and count how many words start with each letter. We will also make a set of result files that store all words starting with each letter.

Review the ASCII character codes for this lab ASCII Table

Read about how files work in C++

Create a new file lab6.cpp.

Write a function int starts_with_letter(string filename, char letter) that takes a file and writes all the words that start with the given letter into a file. The function returns the number of words it put into the file.

Here is an outline for how your function should work.

Instruction Files

Related Questions in computer science category