File input output read text file c++ -
my task write program reads text file, prints statistical results contents output file. start asking user input name of input file, , input name of output file. input file can file containing plain text. want read contents of input file, , print output file following information contents of input file. using ms visual studio assignment , don't know if doing right far think have create .cpp input text file , 1 .cpp output file , 1 execution. kinda lost on creating , using text files too. here got:
#include <iostream> #include <fstream> #include <cctype> #include <cstring> using namespace std; int main() { char filename[100]; // string filenames ifstream input; // file input stream ofstream output; // file output stream { input.clear(); // clear status flags in stream cout << "please enter name of input file.\n"; cout << "filename: "; cin >> filename; input.open(filename); if (!input) cout << "sorry not valid file. try again!\n"; } while (!input); { output.clear(); // clear status flags in stream cout << "please enter name of output file.\n"; cout << "filename: "; cin >> filename; output.open(filename); if (!output) cout << "sorry not 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; } cout << "processing complete\n"; double perc_letters = 0; double perc_white_space = 0; double perc_digits = 0; double perc_other_characters = 0; cout << "\nstatistics 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; }
it shouldn't make difference if put them in different cpp files, thats making structure of code. not 100% sure on how going assignment if using c++ getline() function handy. can like:
while (std::getline(file, line)){ if (line[0] == 'v' && line[1] == ' '){ sscanf_s(&line[0], "v %f, &x);} thats gonna store float value in x on line if line starts with: "v "
Comments
Post a Comment