-2
#include<iostream>
#include<windows.h>
#include<string>
#include<fstream>
using namespace std;
class linklist     //linked list class
{
    struct main_node;
    struct sub_node;

    struct main_node   // main node that only have head pointers in it
    {
        sub_node *head;
        main_node()
        {   head=NULL;  }
    };
    main_node array[26];
    struct sub_node
    {
        double frequency;
        string word;
        sub_node *next;
        sub_node()
        {   frequency=1;    word="";    next=NULL;  }
    };

public:
    void add_node(string phrase)
    {
        char alphabat1=phrase[0];
        if(isupper(alphabat1))
        {
            alphabat1=tolower(alphabat1);
        }
        if(!isalpha(alphabat1))
            return;

        sub_node*temp = new sub_node;
        temp->word = phrase;

        sub_node*current = array[alphabat1-97].head;

        if(current == NULL)
        array[alphabat1-97].head = temp;

        else
        {
            while(current -> next != NULL && phrase != current-> word)
            {   current= current->next; }

            if(current->word == phrase)
                current->frequency++;
            else
                current->next  = temp; //adding words to linklist
        }
    }

    void display()
    {
        for(int i=0;i<26;i++)
        {
        sub_node *temp=array[i].head;
        cout<<char(i+97)<<" -> ";
        while(temp!=NULL)
        {
            cout<<temp->word<<" ("<<temp->frequency<<")  ";
            temp=temp->next;
        }
        cout<<"\n";
        }
    }
void parsing_documents(char *path)
{
    char token[100];
    ifstream read;
    read.open(path);
    do
    {
        read>>token;    // parsing words
        add_node(token);    //sending words to linked list

    }
    while(!read.eof());
        read.clear();
        read.close();
}
void reading_directory()
{
 // code to read multiple files

   HANDLE          hFile;                // Handle to file
   WIN32_FIND_DATA FileInformation;      // File information
   char tempPattern[90];
   strcpy(tempPattern,"*.txt");
   hFile = ::FindFirstFile(tempPattern, &FileInformation);
   long count=0;
   if(hFile != INVALID_HANDLE_VALUE)
   {
        do
        {
            count++;
            cout<<"."<<count;
            this->parsing_documents( FileInformation.cFileName);
        }
        while(TRUE == ::FindNextFile(hFile, &FileInformation));
   } 
    ::FindClose(hFile);

}
};
void main()
{
    linklist member;
    member.reading_directory();
    member.display();
}

私は、50,000 以上のテキスト ファイルを読み込んで単語を解析し、並べ替えた方法でリンク リストに保存する必要があるプロジェクトに取り組んでいます。C++ でコードを作成しました。それは非常に効率的に機能していますが、この点に関して、3000、時には4000のファイルを正しく読み取らないという問題が1つあります。. これがC++の私のコードです。この点で誰かが私を助けてくれれば、とても感謝しています

4

1 に答える 1

1

!read.eof()ネットワークにマウントされたファイルシステムの準備ができていない、ディスクエラー、ファイルを読み取る権限がないなど、ファイルの読み取りエラーではなく、ファイルの終わりのみをチェックします。すべての障害をチェックする必要がwhile(read)あります。これにより、すべてをチェックするためのオーバーロードされた演算子があります。したがって、ファイルが失敗した場合は、ファイルからの読み取りを停止します。また、ファイルから読み取る前に、ステータスを確認する必要があります。そのwhile(read) { ... }ため、do/whileループよりも望ましいです。ループの後、ファイルの終わりに到達しなかったユーザーに警告またはエラーを発行して、!read.eof()特定のファイルを調査できるようにする場合があります。

エラーが発生しやすいので、できるだけ避けてくださいchar *char []あなたはchar[100]を持っています。文字列が100文字より長い場合はどうなりますか? read >> tokenスタックを上書きする可能性があります-を損傷するなどifstream read

std::list<sub_node>ホイールを再発明して再デバッグする必要をなくすために使用することを検討しますか?std :: listがすでにそれを行っているので、次のポインタはもう必要ありません。これにより、デバッグするコードがはるかに少なくなります。

于 2012-09-14T18:00:26.903 に答える