Guest User

Untitled

a guest
Jan 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1.     //
  2.     //  main.cpp
  3.     //  countFile
  4.     //
  5.     //  Created by Arnar 就r Sveinsson on 9/27/12.
  6.     //  Copyright (c) 2012 Arnar 就r Sveinsson. All rights reserved.
  7.     //
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11. #include <ostream>
  12. #include <cstdlib>
  13.  
  14. using namespace std;
  15.     //Declare all functions
  16. void input(char* in_name, char* out_name);
  17. void count_file(ifstream &inFile, char next_char, char prev_char, int &word_count, int &char_count, int &line_count);
  18. void output(ostream &output, int wo_count, int ch_count, int li_count);
  19. int main()
  20. {   //Declare in and out name for filestream and set as an array with maximum 50 characters
  21.     char in_name[50];
  22.     char out_name[50];
  23.         //Declaring all counters, set word and char to 0 but line to 1 because even though you have no words you'll always have 1 line
  24.     int word_count = 0, char_count = 0, line_count = 0;
  25.     char next_char = ' ', prev_char = ' ';
  26.         //Declare the stream to get a file input and output
  27.     ifstream inFile;
  28.     ofstream outFile;
  29.         //Call the input function
  30.     input(in_name,out_name);
  31.         //Open files
  32.     inFile.open(in_name);
  33.     outFile.open(out_name);
  34.         //Check if input file opened
  35.     if (!inFile)
  36.     {
  37.         cout << "Unable to open the file " << in_name;
  38.         exit(1);
  39.     }
  40.         //Call the count function which counts words, characters and lines
  41.     count_file(inFile, next_char, prev_char, word_count, char_count, line_count);
  42.         //Call the output function which writes out the results on the screen and to another file.
  43.     output(outFile, word_count, char_count, line_count);
  44.     output(cout, word_count, char_count, line_count);
  45.         //Close the files
  46.     inFile.close();
  47.     outFile.close();
  48.     return 0;
  49. }
  50.     //Function that takes an input as a string and returns it
  51. void input(char* in_name, char* out_name)
  52. {
  53.     cout << "Write in the full name of the file you want to use as an input file (maximum 50 characters):  ";
  54.     cin >> in_name;
  55.     cout << "Write in the full name of the file you want to use as an output file (maximum 50 characters): ";
  56.     cin >> out_name;
  57. }
  58.     //Count function that counts words, characters and lines
  59. void count_file(ifstream &inFile, char next_char, char prev_char, int &word_count, int &char_count, int &line_count)
  60. {
  61.         //Get the first character
  62.     inFile.get(next_char);
  63.         //If that character isn't EOF then add 1 to line count and char count.
  64.     if(!inFile.eof())
  65.     {
  66.         line_count++;
  67.         char_count++;
  68.     }
  69.         //While there are characters to get add one to char count
  70.     while (inFile.get(next_char))
  71.     {
  72.         char_count++;
  73.             //If that character isn't a space and the previous character is a space then add 1 to word count
  74.         if (!isspace(next_char) && isspace(prev_char))
  75.             word_count++;
  76.         if (next_char == '\n')
  77.                 //If the next character is a newline character then add one to the line count
  78.             line_count++;
  79.             //Make the current char the previous one to make the loop run again if there are more characters.
  80.         prev_char = next_char;
  81.     }
  82. }
  83.  
  84.     //Output function that writes out to a file and to the screen
  85. void output(ostream &output, int wo_count, int ch_count, int li_count)
  86. {
  87.         //Write out the results to an output source
  88.     output << "The input contains:" << endl << li_count << " lines" << endl << wo_count << " words" << endl << ch_count << " characters";
  89. }
Add Comment
Please, Sign In to add comment