我的程序读取时出现问题,从文件中查找包含短语的单词并将结果写入文件。
以下是整个计划:
#include <iostream>
#include <fstream>
#include <array>
#include <string>
using namespace std;
void space();
void intro();
void again(string input3);
bool isRunning = true;
class ReadWriteFile
{
public:
string input;
string input2;
string file;
string tmp;
int numberOfLines = 0;
int counter = 0;
void fileName()
{
cout << " Enter the file name: ";
cin >> input;
file = input + ".txt";
space();
fileLength();
}
string fileLength()
{
ifstream fileStream (file/*.c_str()*/);
if (fileStream)
{
while (fileStream >> tmp)
numberOfLines++;
// cout << numberOfLines;
// space();
fileInput();
}
else
{
cout << " Wrong Input";
space();
fileName();
}
fileStream.close();
}
void fileInput()
{
string arr1 [numberOfLines];
ifstream fileStream (file/*.c_str()*/);
for (int i = 0; i < numberOfLines; i++)
{
fileStream >> arr1 [i];
}
fileStream.close();
orderInput(arr1);
}
void orderInput(string *arr1)
{
string arr2[numberOfLines];
int a = 0;
//string arr2 [numberOfLines];
cout << " Enter a search key phrase: ";
cin >> input2;
space();
for (int i = 0; i < numberOfLines; i++)
{
tmp = arr1[i];
//cout << tmp;
if (tmp.find(input2) != string::npos)
{
arr2[a] = tmp;
cout << " " << tmp << endl;
a++;
}
}
space();
writeFile (arr2);
}
void writeFile (string *arr2)
{
ofstream fileStream ("output.txt");
for (int i = 0; i < numberOfLines; i++)
{
fileStream << arr2[i] << endl;
}
fileStream.close();
cout << " The results are located in the output text file named 'output'" << endl;
}
};
ReadWriteFile rwf;
int main ()
{
string input3;
intro();
while (isRunning = true)
{
rwf.fileName();
cout << "Do you want to run this program again? (y/n): ";
cin >> input3;
again(input3);
}
return 0;
}
void space()
{
string enters = "\n\n\n\n\n\n\n\n\n\n";
string rule =
" --------------------\n ----------";
cout << endl << rule << enters;
}
void intro()
{
cout << endl << endl <<
" ########## ###### ## ## ##########"<< endl <<
" ## ## ## ## ##"<< endl <<
" ## ## #### ##"<< endl <<
" ## ###### #### ##"<< endl <<
" ## ## ## ## ##"<< endl <<
" ## ###### ## ## ##"<< endl <<
" "<< endl <<
" ##### ###### #### ##### ##### ## ##"<< endl <<
" ## ## ## ## ## ## ## ##"<< endl <<
" #### ## ###### ## ## ## ##"<< endl <<
" ## ###### ## ## ## ## #######"<< endl <<
" ## ## ## ## ## ## ## ##"<< endl <<
" ##### ###### ## ## ## ##### ## ##"<< endl <<
" "<< endl <<
" ########## ##### ##### ##"<< endl <<
" ## ## ## ## ## ##"<< endl <<
" ## ## ## ## ## ##"<< endl <<
" ## ## ## ## ## ##"<< endl <<
" ## ## ## ## ## ##"<< endl <<
" ## ##### ##### ######"<< endl << endl;
}
void again(string input3)
{
if (input3 == "y" || input3 == "Y" || input3 == "yes" || input3 == "Yes" || input3 == "YES")
{
}
else if (input3 == "n" || input3 == "N" || input3 == "no" || input3 == "No" || input3 == "NO")
{
isRunning = false;
}
}
以下是我认为导致问题的代码:
void fileInput()
{
string arr1 [numberOfLines];
ifstream fileStream (file/*.c_str()*/);
for (int i = 0; i < numberOfLines; i++)
{
fileStream >> arr1 [i];
}
fileStream.close();
orderInput(arr1);
}
void orderInput(string *arr1)
{
string arr2[numberOfLines];
int a = 0;
//string arr2 [numberOfLines];
cout << " Enter a search key phrase: ";
cin >> input2;
space();
for (int i = 0; i < numberOfLines; i++)
{
tmp = arr1[i];
//cout << tmp;
if (tmp.find(input2) != string::npos)
{
arr2[a] = tmp;
cout << " " << tmp << endl;
a++;
}
}
space();
writeFile (arr2);
}
void writeFile (string *arr2)
{
ofstream fileStream ("output.txt");
for (int i = 0; i < numberOfLines; i++)
{
fileStream << arr2[i] << endl;
}
fileStream.close();
cout << " The results are located in the output text file named 'output'" << endl;
}
编辑:程序冻结在:“结果位于名为'output'的输出文本文件中”
答案 0 :(得分:-2)
更改
<Directory "C:/Users/Me/AppData/Roaming/XAMPP/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
fileLength()和 fileInput()函数的到ifstream fileStream (file/*.c_str()*/);
。