#include "SWindows.h" // set up for windows #include "IOTools.h" // tools for safe I/O #include "Graphics.h" // graphics functions & RGB definition #include "FileTool.h" // file tools #include "Text.h" // fonts and text #include "Delay.h" // delay controls #include // include file stream library //-------------------------------------------------------------------- // File stream library idioms. // // Read the file "data.txt", count the number of characters in it. // Also count newlines, spaces and words separately. // // Use the FileTool package from CoreTools to select a file // using the standard Mac dialog box, and do the same counting. //-------------------------------------------------------------------- // prototypes void main(); // function definitions void main() { BigConsole(); char c; int space_count=0; int newline_count=0; int char_count=0; ifstream fileA ("data.txt"); // create an input file stream fileA and // associate it with the file (in the current folder) // Always check if the file was opened successfully! //-------------------------------------------------------------------- // Idiom 0 : How to check that the stream fileA was successfully // connected to a file //-------------------------------------------------------------------- if ( ! fileA ) { cout << "No such file. Try again." << endl; } else { cout << "File opened successfully" << endl; //-------------------------------------------------------------------- // Idiom 1: Read single chars from the file, one by one, until EOF //-------------------------------------------------------------------- while ( fileA.get(c) ) { // read a char from fileA, put it into c // if EOF is encountered, return false (the loop will finish) if ( c == ' ' ) space_count++; if ( c == '\n') newline_count++; char_count++; } // will terminate at EOF } cout << endl; cout << "File contains " << newline_count << " newlines, " << endl; cout << space_count << " spaces, " << char_count << " characters." << endl; cout << endl; fileA.close(); // close the file ifstream fileB; // create another file stream fileB.open("data.txt"); // open the file again, putting the 'get' pointer at the // beginning of the file if ( !fileB ) { cout << "Could not open file. Aborting" << endl; exit (-1); // Another way of handling unsuccessful attempts at // opening files -- abort, end program. cout << "This line will never be printed because of exit() above!!!" << endl; } else cout << "File re-opened. Counting words: " << endl; int word_count = 0; string str; // the 'new style' C++ string //------------------------------------------------------------------------- // Idiom 2 : Read words (separated by whitespace) from the file, one by one //------------------------------------------------------------------------- while ( fileB >> str ) { // read chars into string str from the file until whitespace ( ' ' or '\n' ) // or EOF is encountered. // return false if unsuccessful, i.e. the 'get' pointer was already at EOF word_count++; cout << str << endl; // print out the word } cout << endl; cout << "File contains "<< word_count << " words." << endl << endl; //-------------------------------------------------------------------- // Now count chars, spaces and newlines in the file of your choice, // selected via the standard Mac dialog box. //-------------------------------------------------------------------- char_count = 0; space_count = 0; newline_count = 0; // reset the counters string s1; // a 'new style' string SelectOldFileName (s1); // brings up a dialog box and lets you select a file // by standard Mac navigation // Takes a reference to a string and puts the full name // of the file selected into the string cout << "You selected the following file:" << endl << s1 << endl; // In order to use the output of SelectOldFileName , you must open the file // with another function from FileTools ! ifstream fileC; // you have to create a file stream OpenFile( fileC, s1 ); // this is it: // OpenFile takes the name of the input stream and the // string containing the (full) pathname (selected by // SelectOldFileName or SelectNewFileName ) // check if the file was succesfully created -- TRY clicking CANCEL in the // file dialog box ! if ( ! fileC ) { cout << "That did not work. Aborting program! " << endl; exit (-1); // aborts program } else cout << "Got it!" << endl; while ( fileC.get(c) ) { // read in char after char if ( c == ' ' ) space_count++; if ( c == '\n') newline_count++; char_count++; } cout << endl; cout << "File " << s1 << " contains " << newline_count << " newlines, " << space_count << " spaces, " << char_count << " characters." << endl; fileC.close(); ///////////////////////////////////////////////////////////////// // // count the number of times a letter occurs in a text // ///////////////////////////////////////////////////////////////// const int NumLetters = 26; int Counter[ NumLetters ]; // the counter array // Counter[0] counts 'a's, Counter[1] counts 'b's etc. for (int i=0; i='a' && ch <= 'z') || (ch >='A' && ch <= 'Z') ){ // ch is a letter if ( ch >='A' && ch <= 'Z' ) // ch is an uppercase letter Counter[ch-'A'] += 1; else Counter[ch-'a'] += 1; } } // print out the counter array for (int i=0; i