Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // countFile
- //
- // Created by Arnar 就r Sveinsson on 9/27/12.
- // Copyright (c) 2012 Arnar 就r Sveinsson. All rights reserved.
- //
- #include <iostream>
- #include <fstream>
- #include <ostream>
- #include <cstdlib>
- using namespace std;
- //Declare all functions
- void input(char* in_name, char* out_name);
- void count_file(ifstream &inFile, char next_char, char prev_char, int &word_count, int &char_count, int &line_count);
- void output(ostream &output, int wo_count, int ch_count, int li_count);
- int main()
- { //Declare in and out name for filestream and set as an array with maximum 50 characters
- char in_name[50];
- char out_name[50];
- //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
- int word_count = 0, char_count = 0, line_count = 0;
- char next_char = ' ', prev_char = ' ';
- //Declare the stream to get a file input and output
- ifstream inFile;
- ofstream outFile;
- //Call the input function
- input(in_name,out_name);
- //Open files
- inFile.open(in_name);
- outFile.open(out_name);
- //Check if input file opened
- if (!inFile)
- {
- cout << "Unable to open the file " << in_name;
- exit(1);
- }
- //Call the count function which counts words, characters and lines
- count_file(inFile, next_char, prev_char, word_count, char_count, line_count);
- //Call the output function which writes out the results on the screen and to another file.
- output(outFile, word_count, char_count, line_count);
- output(cout, word_count, char_count, line_count);
- //Close the files
- inFile.close();
- outFile.close();
- return 0;
- }
- //Function that takes an input as a string and returns it
- void input(char* in_name, char* out_name)
- {
- cout << "Write in the full name of the file you want to use as an input file (maximum 50 characters): ";
- cin >> in_name;
- cout << "Write in the full name of the file you want to use as an output file (maximum 50 characters): ";
- cin >> out_name;
- }
- //Count function that counts words, characters and lines
- void count_file(ifstream &inFile, char next_char, char prev_char, int &word_count, int &char_count, int &line_count)
- {
- //Get the first character
- inFile.get(next_char);
- //If that character isn't EOF then add 1 to line count and char count.
- if(!inFile.eof())
- {
- line_count++;
- char_count++;
- }
- //While there are characters to get add one to char count
- while (inFile.get(next_char))
- {
- char_count++;
- //If that character isn't a space and the previous character is a space then add 1 to word count
- if (!isspace(next_char) && isspace(prev_char))
- word_count++;
- if (next_char == '\n')
- //If the next character is a newline character then add one to the line count
- line_count++;
- //Make the current char the previous one to make the loop run again if there are more characters.
- prev_char = next_char;
- }
- }
- //Output function that writes out to a file and to the screen
- void output(ostream &output, int wo_count, int ch_count, int li_count)
- {
- //Write out the results to an output source
- output << "The input contains:" << endl << li_count << " lines" << endl << wo_count << " words" << endl << ch_count << " characters";
- }
Add Comment
Please, Sign In to add comment