-
February 13th, 2013, 02:43 PM
#16
Re: I cannot print on an output file
Your array is defined with a fixed limit of 1000 characters. What if the input file has more than 1000 characters? If it has more than you overrun the a array and overwrite whatever happens to be following in memory. This is the well-known buffer-overrun security issue! If you must read the file into an array first then you should check that you are not trying to write past the end of the array
Code:
#define FILESIZE 1000
float Frequency(char chr, float num, char a[FILESIZE]);
void Frequency_file2(char chr, float num, char a[FILESIZE]);
float counts(char chr, float num, char a[FILESIZE]);
....
while ((file1.eof() == false) && (counter < FILESIZE)) {
file1 >> a[counter++];
}
But what if the file has 1 million characters? 10 billion characters? How do you know what size to dimension the array? It would be better to do the analysis as each character is read from the file. You are only counting lowercase characters so have an array with 26 elements. Use islower(..) to check if a character read is lowercase and if it is use something like count[ch - 'a']++ (assuming ASCII). You then won't need the counts function.
You also don't need to terminate the alphabet and a arrays with \0 as they are not being treated as null-terminated strings but just as arrays of char.
Also, you are using multi-if statements for the analysis choice. A switch statement would be preferable. It makes the code more readable.
Code:
switch (choice) {
case 1:
....
break;
case 2:
.....
break;
case 0:
break;
default:
cout << "Not a valid option." << endl;
break;
}
-
February 13th, 2013, 03:42 PM
#17
Re: I cannot print on an output file
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void menu0();
void menuA();
int main() {
ifstream file1;
ofstream file2;
bool done = false;
string filename;
int choice,
num = 0,
lc = 0,
counts[26] = {0};
char ch,
char_to_check;
menu0();
do {
cout << "Please type file name (<CR> to exit): ";
getline(cin, filename, '\n');
if (filename.empty()) {
return (0);
}
file1.open(filename.c_str());
if (file1.fail()){
file1.clear();
cout << "Could not open the file '" << filename << "'" << endl;
} else {
done = true;
}
} while (!done);
while (file1.eof() == false) {
file1 >> ch;
num++;
if (islower(ch)) {
lc++;
counts[ch - 'a']++;
}
}
file1.close();
cout << "The number of all characters in the file is " << num << endl;
cout << "of which " << lc << " are lowercase." << endl;
do {
menuA();
cout << "Choose: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Please give character to check: ";
cin >> char_to_check;
if (islower(char_to_check)) {
cout << "Character " << char_to_check << " appears " << counts[char_to_check - 'a'] << " times in the text." << endl;
cout << "It has a frequency of " << (float)counts[char_to_check - 'a'] / (float)num << "." << endl;
} else {
cout << "Character " << char_to_check << " is not lowercase" << endl;
}
break;
case 2:
file2.open("report.txt");
if (!file2) {
cout << "Cannot create file 'report.txt'" << endl;
} else {
cout << "Report stored in file: report.txt " << endl;
for (char p = 'a'; p <= 'z'; p++) {
file2 << "Character " << p << " appears " << counts[p - 'a'] << " times in the text." << endl;
file2 << "It has a frequency of " << (float)counts[p - 'a'] / (float)num << "." << endl;
file2 << endl;
}
file2.close();
}
break;
case 0:
break;
default:
cout << "Not a valid option." << endl;
break;
}
} while (choice != 0);
cout << "Now quitting.." << endl << endl;
return 0;
}
void menu0() {
cout << "This program accepts a text file from a user and performs" << endl;
cout << "an analysis of the letters and characters used by the author." << endl << endl;
}
void menuA() {
cout << "Choose" << endl;
cout << "1. To perform analysis on a single character." << endl;
cout << "2. To write analysis for all alphabet in a file." << endl;
cout << "0. To quit." << endl;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|