vendredi 27 mars 2015

Text File Analyzer c++ [on hold]

My question is how do I get a text file for input/output on my computer. This is my code so far which makes since to me but I need to get a text file! How do I make one...How do I access one...How can I get it to work in my program to read characters, letters, digits, etc...Do I need permission's to access?



#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
using namespace std;

int main()
{
char filename[30]; // a string for filenames
ifstream input; // a file input stream
ofstream output; // a file output stream

do
{
input.clear(); // to clear status flags in the stream
cout << "Please enter the name of the input file.\n";
cout << "Filename: ";
cin >> filename;
input.open(filename);
if (!input)
cout << "Sorry not a valid file. Try again!\n";
} while (!input);


do
{
output.clear(); // to clear status flags in the stream
cout << "Please enter the name of the output file.\n";
cout << "Filename: ";
cin >> filename;
output.open(filename);
if (!output)
cout << "Sorry not a valid file. Try again!\n";
} while (!output);


char ch;
int characters = 0;
int letters = 0;
int white_space = 0;
int digits = 0;
int other_characters = 0;
int uppercase = 0;
int lowercase = 0;

while (!input.eof()); // while not end of input file
{
input.get(ch);

if(isalnum(ch))
characters++;
output << ch;

if(isalpha(ch))
++letters;
output << ch;

if(isspace(ch))
++white_space;
output << ch;

if(isdigit(ch))
++digits;
output << ch;

if(ispunct(ch))
++other_characters;
output << ch;

}
double perc_letters = 0;
double perc_white_space = 0;
double perc_digits = 0;
double perc_other_characters = 0;

cout << "\nStatistics for file:" << filename << endl;
cout << "--------------------------------------------------\n";

cout << "Total # of charcters in file:\t " << characters << endl;
cout << "\nCategory\tHow many in file\t% of file\n";
cout << "--------------------------------------------------\n";
cout << "Letters\t" << letters << "\t" << perc_letters << "%" << endl;
cout << "White space\t" << white_space << "\t" << perc_white_space << "%" << endl;
cout << "Digits\t" << digits << "\t" << perc_digits << "%" << endl;
cout << "Other characters\t" << other_characters << "\t" << perc_other_characters << "%" << endl;
cout << "\n";

input.close();
output.close();

return 0;

}

Aucun commentaire:

Enregistrer un commentaire