ファイルを読み込んだ後に単語を要求する必要があります。その後、その単語を行単位で表示する必要があります。また、私はchar配列でこれをチェックする必要があります。私の 出力の例を確認することができます。あなたが見ることができるようにテキストファイル内の単語の頻度を行単位で検索するC++
Line 2: 1 occurrence(s)
line 4: 2 occurrence(s)
Line 7: 1 occurrence(s)
私はな長さを検索文字列によって行な長さを分け、これが発生するsearchStringのpossiblityの最大時間です。だから、私は出現を表示する必要がありますが、私のコードはこの分裂をオカレンスとして示しています。これについて私に助けてくれますか?
#include <iostream>
#include <string>
#include <fstream>
#include <istream>
using namespace std;
int number_of_lines = 1;
void numberoflines();
unsigned int GetFileLength(std::string FileName)
{
std::ifstream InFile(FileName.c_str());
unsigned int FileLength = 0;
while (InFile.get() != EOF) FileLength++;
InFile.close();
cout<<"Numbers of character in your file : "<<FileLength<<endl;
return FileLength;
}
int main()
{
string searchString, fileName, line;
int a;
string *b;
char *c,*d;
int wordCount = 0, count = 0,count1=0;
cout << "Enter file name : " << endl;
cin >> fileName;
GetFileLength(fileName);
cout << "Enter a word for searching procces : " << endl;
cin >> searchString;
ifstream in (fileName.c_str(), ios::in);
d= new char[searchString.length()+1];
strcpy(d,searchString.c_str());
a=GetFileLength(fileName);
b= new string [a];
if(in.is_open()){
while(!in.eof()){
getline(in,line);
c= new char[line.length()+1];
count++;
strcpy(c,line.c_str());
count1=0;
for (int i = 0; i < line.length()/searchString.length(); i++)
{
char *output = NULL;
output = strstr (c,d);
if(output) {
count1++;
}
else count1--;
}
if(count1>0){cout<<"Line "<<number_of_lines<<": "<<count1<<" occurrence(s) "<<endl;}
number_of_lines++;
if (count==10)
{
break;
}
}
numberoflines();
}
return 0;
}
void numberoflines(){
number_of_lines--;
cout<<"number of lines in text file: " << number_of_lines << endl;
}
*私はほとんどこれをしましたが、私のプログラムは文字数を示しています*私には分かりません。その文はどういう意味ですか? – NathanOliver
私はこのコードでメモリリークを無視しようとしていますが、恐ろしく失敗しています。 – WhozCraig
@WhozCraigメモリリークは、ファイルの長さを取得する方法と比べて何もありません。 – Overv