#include"std_lib_facilities.h"
int main()
try
{
cout << "Please enter the name of file you want to find: ";
string find_file;
cin >> find_file;
ifstream fifs(find_file.c_str());
if (!fifs)error("can't open the file : ", find_file);
cout << "Please enter the word you want to find: ";
string find_word;
cin >> find_word;
cout << "Please enter the name of file you want to write: ";
string write_file;
cin >> write_file;
ofstream wofs(write_file.c_str());
if (!wofs)error("can't open the file : ", write_file);
vector<string>file_to_string_vector;
string file_string;
while (getline(fifs, file_string))file_to_string_vector.push_back(file_string);
vector<vector<string>>desperate_strings(file_to_string_vector.size());
for (int i = 0; i < file_to_string_vector.size(); i++)
{
stringstream string_line (file_to_string_vector[i]);
while (string_line>> file_string)desperate_strings[i].push_back(file_string);
}
for (int i = 0; i < desperate_strings.size(); i++)
{
for (int j = 0; j < desperate_strings[i].size(); j++)
{
if (desperate_strings[i][j] == find_word)
{
wofs << "The lins is " << i + 1<<" :\n";
for (int q = 0; q < desperate_strings[i].size(); q++)
wofs << desperate_strings[i][q] << ' ';
wofs << '\n' << '\n';
}
}
}
}
catch (exception& e) {
cerr << "error: " << e.what() << '\n';
return 1;
}
catch (...) {
cerr << "Oops: unknown exception!\n";
return 2;
};
C++程序设计原理与实践第10章习题12
最新推荐文章于 2023-04-30 08:41:30 发布