So I'm writing a program for an assigment that pulls words out of a fixed format text file (which is the reverse index of a directory, and is formatted in the following way:
<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>
and stores them in a linked list. Each word is stored in a struct which contains the word, a pointer to another list object [which contains the file descriptor and the number of occurences, and a pointer to the next node in the file descriptor list], and a pointer to the next node in the list.
Eventually, Instead of creating a linked list out of these nodes, I will hash them, to make a search function.
Currently, though, I am receiving several erros while trying to compile my code. It is included here:
#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;
}
which includes the file "hashtable.h" which follows:
#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
So whenever I try to compile, I recieve a whole huge mess of errors. The main error I seem to be getting though, is:
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
I've tried playing around with how I declare my structs, and how I create my list, but I can't seem to figure out what's causing these errors. I know it's a lot of code to sift through, but if anyone can point out what I'm doing wrong, I would be much obliged.
Thanks.
hashtok
is a pointer. So temphashhead
is a pointer to a pointer. So you have to say (*temphashhead)->next
.
(This is also entirely pointless, as temphashhead
only points a local variable, and you never need to reseat the pointer. So just say hashtok temphashhead = hashhead;
.)