想要读取超过 50,000 个 TXT 文件并将它们保存在 C++ 的链表中
want to read more than 50,000 txt files and save them in linked list in C++
#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。我已经搜索了很多,但我无法成功找到我的错误。.这是我的代码C++ 如果有人在这方面帮助我,我将不胜感激
!read.eof()
只检查文件末尾,而不检查读取文件的错误,例如网络挂载的文件系统未准备好、磁盘错误或缺少读取文件的权限。 您应该检查所有故障,while(read)
它有一个重载的运算符来为您检查所有内容。 因此,如果文件失败,您将停止尝试从中读取。 在尝试读取文件之前,还应检查状态。 因此,while(read) { ... }
比 do/while 循环更可取。 循环后,您可能会向未到达文件末尾的用户发出警告或错误!read.eof()
以便他们可以调查该特定文件。
尽量避免char *
并尽可能char []
,因为这很容易出错。 你有一个字符[100]。 如果字符串长度超过 100 个字符,会发生什么情况? read >> token
可能会覆盖堆栈 - 例如损坏ifstream read
。
考虑使用std::list<sub_node>
以避免重新发明和重新调试轮子? 您将不再需要下一个指针,因为 std::list 已经为您执行此操作。 这将留下更少的代码来调试。
相关文章:
- C++模板来检查友元函数的存在
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 既然存在危险,为什么项目要使用-I include开关
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 我们可以访问一个不存在的联盟的成员吗
- C++:对不存在的命名空间使用命名空间指令
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- C++quit()函数中可能存在作用域问题
- C++擦除(如果存在)
- 在C++中将类(带有Vector成员)保存为二进制文件
- 使用导入的图像保存在 QT 中的 QLabel 中,保存 GUI 的状态
- 阵列不能两次将相同的名称保存,当要添加新字符串时,程序应检查以查看该名称是否已经存在
- 方法,该方法调用所有基类的同名方法(如果存在),并将返回值保存到列表中
- 什么设计模式将数据部分保存在持久存储 (SQL) 中,部分保存在 RAM(内存)中
- 是否存在可用于保存任何枚举值的整型?
- 将对象指针保存在 STL 容器中是否比将对象本身保存更好
- 从控制台保存信息,然后重新打开时,信息仍然存在