Why won't my program continue after I ask for the text file? Once I run the program, for some reason I doesn't seem as if it looks for the file, nor does it output the contents of the file.
what can be done to fix this.
I'm trying to input a text file with a string of paths. And then from there I parse through this string and put it to a link list. and then create a search function afterwards.
output:
enter text file
warning: this program uses gets(), which is unsafe.
a1.txt
text file: path.txt
a/a1.txt
a/a2.txt
a/b/b3.txt
a/b/b4.txt
a/c/c4.txt
a/c/c5.txt
a/c/d/d6.txt
a/c/d/g
a/c/d/h
a/c/e/i/i7.txt
a/c/f/j/k/k8.txt
code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct sMyPath{
char *element;
struct sMyPath *next;
} tMyPath;
int main(void)
{
FILE *pFile;
pFile = fopen("path.txt", "r");
char inputstr[1024];
tMyPath *curr, *first = NULL, *last = NULL;
if (pFile != NULL)
{
while(!feof(pFile))
{
fgets(inputstr, sizeof(inputstr), pFile);
}
fclose(pFile);
}
else
{
printf("Could not open the file.\n");
}
char *token = strtok(inputstr, "\\");
while (token != NULL)
{
if(last == NULL){
first = last = malloc (sizeof (*first));
first -> element = strdup (token);
first -> next = NULL;
} else {
last -> next = malloc (sizeof (*last));
last = last -> next;
last -> element = strdup (token);
last -> next = NULL;
}
token = strtok(NULL, "\\");
}
char pathU[20];
printf("enter text file\n");
gets(pathU);
while(first != NULL)
{
if(strcmp (first -> element,pathU)==0)
{
FILE *nFile;
char texxt[300];
nFile = fopen(pathU, "r");
while (!feof(nFile))
{
fgets(texxt, 300, nFile);
printf("Theses are the contents\n");
printf("%s", texxt);
}
}
else if(first == NULL)
{
printf("invalid file name\n");
}
else
{
first = first -> next;
}
}
return 0;
}