Hello,
Basically, what my code has so far is just trying to read from a file on the command line and index the words of the file into an array of tableWord (which is a struct that stores the word and the line it occurs in). I get a segmentation fault on line 88 (commented). I'm attaching the entire code, however, just focus on main and wordIndex functions. My issue is probably just with understanding how memory allocation and functions work together in c because when I did this very same thing all in main I had no issues. Hope I wasn't too confusing, thanks for the help!
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct {
char *s;
int line;
} tableWord;
typedef int bool;
#define TRUE 1;
#define FALSE 0;
int len(char*);
bool isEqual(char*, char*);
FILE *openFile(int argc, char* argv[]);
int getNumWords(FILE*, int*);
int wordIndex(char*, tableWord*, int);
int copyFileToString(FILE*, char*);
int main(int argc, char* argv[])
{
char curc;
char *s;
int numWords, sLength=0;
tableWord *tW;
FILE *f;
f = openFile(argc, argv);
numWords = getNumWords(f, &sLength);
/*Allocate memory for tableWords array*/
if((tW = (tableWord*)malloc(sizeof(tableWord)*numWords)) == NULL)
{
printf("unable to allocate space for new struct");
return 1;
}
/*Allocate memory for s*/
if ((s=(char*)malloc(sLength+1))==NULL)
{
printf("unable to allocate space for s\n");
return 1;
}
copyFileToString(f, s);
wordIndex(s, tW, sLength);
int i;
for(i=0; i<numWords;i++)
{
printf("Word: %s Line: %d\n", tW[numWords].s, tW[numWords].line);
}
return 0;
}
int wordIndex(char *s, tableWord *tW, int sLength)
{
int i, j, iSave, curLine = 1, wordCount=0, cCount=0;
bool inWS = TRUE;
for(i=0; i<sLength; i++)
{
if(inWS) //If in white space.
{
if(s[i] > 32) //If enter a word.
{
iSave = i; //Store current spot on s.
inWS = FALSE; //Now out of whitespace.
}
}
else //If not in white space.
{
if(s[i] < 33) //If enter white space.
{
tW[wordCount].line = curLine;
if((tW[wordCount].s = (char*)malloc(i-iSave+1) == NULL))
{
printf("Unable to allocate space for struct word %d", wordCount);
return 1;
}
for(j=iSave; j<i; j++)
{
tW[wordCount].s[cCount++] = s[j]; //Segmentation fault.
}
tW[wordCount].s[cCount] = '\0';
cCount = 0;
wordCount++;
}
}
if(s[i] == '\n')
{
curLine++;
}
i++;
}
return 0;
}
int copyFileToString(FILE *f, char *s)
{
char curc;
int sCount = 0;
while (fscanf(f, "%c", &curc) != EOF) //Reads next character from fil$
{
s[sCount++] = curc;
}
s[sCount] = '\0';
return 0;
}
int getNumWords(FILE *f, int *sL)
{
int numWords=0;
char curc;
bool inWS = TRUE;
while(fscanf(f, "%c", &curc) != EOF)
{
(*sL)++;
if (inWS)
{
if (curc > 33)
{
numWords++;
inWS = FALSE;
}
}
else
{
if (curc < 33)
inWS = TRUE;
}
}
rewind(f);
return numWords;
}
FILE *openFile(int argc, char* argv[])
{
if(argc == 1)
{
printf("No file name specified!\n");
}
FILE *f;
f = fopen(argv[1], "r");
return f;
}
int len(char *s)
{
char *t=s;
while (*t!='\0')
t++;
return t-s;
}
bool isEqual(char *s1, char *s2)
{
int i = 0;
if(len(s1) != len(s2))
return FALSE;
for(; i<len(s1); i++)
{
if(s1[i] != s2[i])
return FALSE;
}
return TRUE;
}