Reading Files using Loops and Functions in C Task 1 Write a

Reading Files using Loops and Functions in C++

Task 1

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.

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 call start_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.

Hints: Use ascii codes to account for both upper and lower case letters. The below code shows you how to generate a file name to use.

        string res_name=\"results_\";

                res_name+=letter;

                res_name+=\".txt\";

Task 2

Write your main program. Ask the user for the name of a file. Open the file and count how many words starting with each letter A-Z appear. The output is the total number of words that start with each letter. The program should also make a file for each letter with the actual words found, one word per line. When the program exists, you should have 26 new files with results.

Task 3

Go to Project Gutenberg (http://www.gutenberg.org) and download a plain text copy of \"Fundamental Principles of the Metaphysic of Morals\" by Immanuel Kant. Run your program on it. Books from Project Gutenberg have copyright and additional material at the beginning and end, just leave this in the file as it is for this lab.

Example Run

Solution

#include <iostream> // library that contain basic input/output functions

#include <fstream> // library that contains file input/output functions

using namespace std;

int main()

{

int ocurrences_count = 0;

char word[20]; //this array will save user input

int array_size = 1024; // define the size of character array

                char * array = new char[array_size]; // allocating an array of 1kb

                int position = 0; //this will be used incremently to fill characters in the array

//prompting user to enter a word to be search in the file

cout << \"Please enter the word to search in file : \";

cin.getline(word,19); //reading user input of max 19 characters because our word array size in 20

int word_size = 0;

//this loop is calculating the length of input word

for(int i = 0; word[i] != \'\\0\'; i++)

{

    word_size++;

}

                ifstream fin(\"test.txt\"); //opening an input stream for file test.txt

                /*checking whether file could be opened or not. If file does not exist or don\'t have read permissions, file

stream could not be opened.*/

if(fin.is_open())

                {

    //file opened successfully so we are here

    cout << \"File Opened successfully!!!. Reading data from file into array\" << endl;

    //this loop run until end of file (eof) does not occur

                                while(!fin.eof() && position < array_size)

                                {

                                                fin.get(array[position]); //reading one character from file to array

                                                position++;

                                }

                                array[position-1] = \'\\0\'; //placing character array terminating character

    //this loop is searching for the word in the array

                                for(int i = 0; array[i] != \'\\0\'; i++)

                                {

                                                for(int j = 0; word[j] != \'\\0\' && j < 20 ; j++)

      {

        if(array[i] != word[j])

        {

          break;

        }

        else

        {

          i++;

          if(word[j+1] == \'\\0\')

          {

            cout << \"Word Found in File at position \" << (i-word_size) << endl;

            ocurrences_count++;

          }

        }

      }

                                }

    cout << \"Total occurences found : \" << ocurrences_count << endl;

                }

                else //file could not be opened

                {

                                cout << \"File could not be opened.\" << endl;

                }

                return 0;

}

Reading Files using Loops and Functions in C++ Task 1 Write a function int starts_with_letter(string filename,char letter) that takes a file and writes all the
Reading Files using Loops and Functions in C++ Task 1 Write a function int starts_with_letter(string filename,char letter) that takes a file and writes all the
Reading Files using Loops and Functions in C++ Task 1 Write a function int starts_with_letter(string filename,char letter) that takes a file and writes all the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site