所以我正在编写一个程序,用于从固定格式文本文件(这是目录的反向索引)中提取单词,并按以下方式格式化:
<list> a // word found in indexed files
0: 1 // in the format <file descriptor>: <number of occurences in file>
1: 1
</list>
<list> b
0:1
</list>
<file>
0: file1.txt //in the format <file descriptor>: <full file path>
1: testdir/file2.txt
</file>
并将它们存储在链表中。每个单词都存储在一个结构中,该结构包含单词,指向另一个列表对象的指针[包含文件描述符和出现次数的指针,以及指向文件描述符列表中下一个节点的指针],以及指向下一个单词的指针列表中的节点。
最终,我不会在这些节点中创建链接列表,而是将它们哈希,以创建搜索功能。
目前,我在尝试编译代码时收到了几个错误。它包括在这里:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "errorchecker.h"
#include "hashtable.h"
int searchthroughfileandcreatenodes(fplist fphead, hashtok hashhead, char * filename)
{
FILE *fp;
fp = fopen(filename, "r");
if(fp == NULL) //prints an error message if file does not exist
fatal(": Input file does not exist");
hashtok * temphashhead = &hashhead;
fplist * tempfphead = &fphead;
char word[256];
char* file[256];
int f = 0;
int count = 7;
int i = 0;
//This loop is respinsible for creating a linked list of all of the
while (fgets(word, 256, fp) != NULL) //words, the files they appear in (represented by a numerical code),
{ //the number of times they appear in each file. It also creates a linked
if(word[0] == '<') //list of files represented in the input file, and sotres them. along
{ //with the numerical code used to describe them in the input file.
if(word[1] == 'l')
{
while(word[count] != '\0')
{
strcpy(file[i], (char*)word[count]);
count++;
i++;
}
count = 7;
temphashhead->next = (hashtok)malloc(sizeof(hashtok));
temphashhead->next->file = (filenumber*)malloc(sizeof(filenumber));
temphashhead->next->file->next = NULL;
temphashhead->next->next=NULL;
temphashhead->next->wordtok =(char*)malloc(sizeof(char) * i + 1);
temphashhead = temphashhead->next; //the function enters this loop when it encounters a <l sequence in the
strcpy(temphashhead->wordtok, file); //input file, which indicates the starts of a new words, and a new
i = 0; //list of files which the word contains. It stores the word into a linked list node,
fgets(word, 256, fp); //then creates another linked list coming off of the original node which
while(word[0] != '<') //contains a list of the file number descriptors the word is contained in, and
{ //the number of occurences in each file.
temphashhead->file->fileno = (int) word[1];
temphashhead->file->numoccurences = (int) word[4];
temphashhead->file->next = (filenumber*)malloc(sizeof(filenumber));
temphashhead->file = hashhead->file->next;
fgets(word, 256, fp);
}
}
else if(word[1] == 'f') //Here, a loop is entered if the '<f' sequence is encountered, which indicates the
{ //start of the list of files and file descriptors in the input file. Thus, a list of
while(fgets(word, 256, fp) != NULL) //filenames is created, which contains their file descriptors for later reference.
{
count = 2;
{
tempfphead->next = (fplist)malloc(sizeof(fplist));
tempfphead->next->next = NULL;
tempfphead->next->filepath = NULL;
tempfphead = tempfphead->next;
while(word[count] != NULL)
{
strcpy(file[i], word[count];
i++;
count++;
}
tempfphead->filepath = (char*) malloc(sizeof(char)*i + 1);
strcpy(head->filepath, file);
i = 0;
}
}
}
close(fp);
return 1;
}
}
}
int main(int argc, char *argv[])
{
hashtok hashhead = hashtokcreate(); //Creation of new list objects: list to hold the words in the file, and list to hold the filenames in the file.
fplist fphead = fplistcreate();
int stfcn = searchthroughfileandcreatenodes(fphead, head, &argv[1]);
char string[256];
char input[256];
printf( "search>" );
fgets ( string, 256, stdin ); //Here, the fgets function is used in conjunction with the 'stdin' command to retrieve
while(string[0] != 'q') //command line input from the user, and buffer the input into a temporary string. Then,
{ //the string is tokenized, and each individual token is copied into a linked list, which,
char selection[3]; //depending on the search parameter (sa or so), will create a new list of the corresponding type
char * token; //which will contain the tokens, and will contain empty lists of which filepaths the search
int i = 0; //targets are present in, which will be filled in the a later function (sosearch or sasearch,
while(i <=1) //which correpsond to the inputs 'so' and 'sa'.
{
strcpy(&selection[i], &string[i]);
i++;
}
if(strcmp(selection, "so") == 0)
{
sotoken sohead = socreate();
socreatelist(&sohead, &string);
sosearch(&fphead, &head, &sohead);
}
else if(strcmp(selection, "sa") == 0)
{
sotoken sahead = sacreate();
sacreatelist(&sahead, &string);
sasearch();
}
else
{
printf("usage -> search> <so|sa|q> <words to search for>\n/t\t<so>->search for files that contain any combination of the target words\n\t\t<sa>->search for words containing exact string\n\t\t<q>->quit program\n");
}
clear(selection);
}
return;
}
/*hashtokcreate() and fplistcreate() functions create a new sotoken or satoken list object, and set all intial values to NULL*/
hashtok hashtokcreate()
{
hashtok head = (hashtok)malloc(sizeof(hashtok));
hashtok->file = (filenumber)malloc(sizeof(filenumber));
hashtok->file->next = NULL;
hashtok->next = NULL;
hashtok->wordtok = NULL;
return head;
}
fplist fplistcreate()
{
fplist head = (fplist)malloc(sizeof(fplist));
head->filepath = NULL;
head->next = NULL;
return head;
}
其中包含以下文件“hashtable.h”:
#ifndef HASHTABLE_H
#define HASHTABLE_H
typedef unsigned int (*HashFunctionT) (char* string, int upperbound);
struct filepathlist //Will contain the filepaths represented in the file, and the numerical codes they are represented by.
{
int pathnumber;
int numoccurences;
char * filepath;
struct filepathlist * next;
};
typedef struct filepathlist * fplist;
struct filenumber_ //Will contain the file descriptors of the files that words are found in, and the number of occurences each word appears in
{ //the file
struct filenumber * next;
int fileno;
int numoccurences;
};
typedef struct filenumber_ * filenumber;
struct Hashtoken //Will contain a list of all of the words present in the input file, and their corresponding appearances in files.
{
char * wordtok;
filenumber * file;
struct Hashtoken * next;
};
typedef struct Hashtoken* hashtok;
struct sotokens //Will contain a list of all of the search targets that were enetered with the parameter 'so', and their corresponding
{ //appearances in files.
char * soword;
filenumber * files;
struct sotokens * next;
};
typedef struct sotokens* sotoken;
struct satokens //Will contain a list of all of the search targets that were enetered with the parameter 'sa', and their corresponding
{ //appearances in files.
char* saword;
filenumber files;
struct satokens * next;
};
typedef struct satokens* satoken;
fplist fplistcreate();
hashtok hashtokcreate();
sotoken socreate();
satoken sacreate();
sotoken socreatelist(sotoken * head, char * searchtargets);
satoken sacreatelist(satoken * head, char * searchtargets);
#endif
因此,每当我尝试编译时,我都会收到大量错误。我似乎得到的主要错误是:
User@root:~/test$ gcc -o s search.c
search.c: In function ‘searchthroughfileandcreatenodes’:
search.c:38:22: warning: cast to pointer from integer of different size
search.c:43:17: error: request for member ‘next’ in something not a structure or union
search.c:44:17: error: request for member ‘next’ in something not a structure or union
search.c:45:17: error: request for member ‘next’ in something not a structure or union
search.c:46:17: error: request for member ‘next’ in something not a structure or union
search.c:47:17: error: request for member ‘next’ in something not a structure or union
search.c:48:32: error: request for member ‘next’ in something not a structure or union
search.c:49:24: error: request for member ‘wordtok’ in something not a structure or union
search.c:49:5: warning: passing argument 2 of ‘strcpy’ from incompatible pointer type
/usr/include/string.h:128:14: note: expected ‘const char * __restrict__’ but argument is of type ‘char **’
search.c:54:18: error: request for member ‘file’ in something not a structure or union
search.c:55:18: error: request for member ‘file’ in something not a structure or union
search.c:56:18: error: request for member ‘file’ in something not a structure or union
search.c:57:18: error: request for member ‘file’ in something not a structure or union
search.c:57:41: error: request for member ‘next’ in something not a structure or union
search.c:67:17: error: request for member ‘next’ in something not a structure or union
search.c:68:17: error: request for member ‘next’ in something not a structure or union
search.c:69:17: error: request for member ‘next’ in something not a structure or union
search.c:70:30: error: request for member ‘next’ in something not a structure or union
我已经尝试过如何声明我的结构,以及如何创建我的列表,但我似乎无法弄清楚是什么导致了这些错误。我知道要筛选很多代码,但如果有人能指出我做错了什么,我会非常感激。
感谢。
答案 0 :(得分:4)
hashtok
是一个指针。所以temphashhead
是指向指针的指针。所以你必须说(*temphashhead)->next
。
(这也完全没有意义,因为temphashhead
只指向一个局部变量,你永远不需要重新设置指针。所以只需说hashtok temphashhead = hashhead;
。)
答案 1 :(得分:2)
这就是为什么使用像这样的typedef是坏。他们隐藏意图。在您需要时输入*
并不难。
跳出来的是:
int searchthroughfileandcreatenodes(fplist fphead, hashtok hashhead,
char * filename)
hashtok
是struct Hashtoken*
然后你这样做:
hashtok * temphashhead = &hashhead;
如果剥离typedef,那么:
sturct Hashtoken **temphashhead = &hashhead;
现在你有一个指向的指针,然后尝试将其用作指针:
temphashhead->next
编译错误随之而来。