Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading a text file into a Linked List

Status
Not open for further replies.

smaniraja

Programmer
Feb 19, 2001
129
IN
I have seen a new thread requesting a program to load the string in a text file to a linked list and with search and replace options. The file to read, the string to be searched and the string to be replaced should be as a command line argument to that program.

If the search string found in the list the replace that with the replace string. If the replace string not given the delete the fond nodes in the list and finally write the results into a separate file.

I furget the Thread or unable to locate that now ...

The following program will solve this problem.

Regard
Maniraja S

#include <stdio.h>
#include <malloc.h>
struct SList
{
char word[25];
struct SList * nextAddress;
};

struct SList * load_to_list(FILE *);
struct SList * read_to_node(FILE *);
struct SList * search_string(struct SList *, char *, char *);
struct SList * put_to_file(struct SList *);

int main(int c, char *a[])
{
FILE *fp;
struct SList *mainNode = NULL;
char sstring[25], *rstring;
if( c <= 1)
{
printf(&quot;\nEnter the File name and two strings...\n&quot;);
exit(0);
}
if( (fp = fopen(a[1], &quot;r&quot;)) == NULL)
{
perror(&quot;Unable to Open the file&quot;);
exit(1);
}
mainNode = load_to_list(fp);
strcpy(sstring, a[2]);
if(c <= 3) rstring = NULL;
else
{
rstring = (char *) malloc(strlen(a[3]) + 1);
strcpy(rstring, a[3]);
}
mainNode = search_string(mainNode, sstring, rstring);
mainNode = put_to_file(mainNode);
return 0;
}

struct SList * load_to_list(FILE *fp)
{
struct SList *start, *node=NULL;

start = read_to_node(fp);
node = start;
while(node != NULL)
{
node -> nextAddress = read_to_node(fp);
node = node ->nextAddress;
}
return start;
}

struct SList * read_to_node(FILE *fp)
{
struct SList *temp = (struct SList *) malloc(sizeof(struct SList));
fscanf(fp, &quot;%s&quot;, temp->word);
temp->nextAddress = NULL;
if(feof(fp))
{
free(temp);
return NULL;
}
return temp;
}

struct SList * search_string(struct SList *node, char * sstring, char * rstring)
{
struct SList *pre, *tmp;
if(rstring == NULL)
{
for(tmp = node, pre = node; tmp != NULL;)
{
if(strcmp(tmp->word, sstring) == 0)
{
if(tmp == node)
{
pre = node = node ->nextAddress;
free(tmp);
tmp = node;
}
else
{
pre->nextAddress = tmp->nextAddress;
free(tmp);
tmp = pre->nextAddress;
}
}
else
{
pre = tmp;
tmp = tmp->nextAddress;
}
}
}
else
for(tmp = node; tmp != NULL; tmp = tmp->nextAddress)
if(strcmp(sstring, tmp->word) == 0) strcpy(tmp->word, rstring);
return node;
}

struct SList * put_to_file(struct SList *node)
{
FILE *fp;
struct SList * tmp;
fp = fopen(&quot;result.txt&quot;, &quot;w&quot;);
for(tmp = node; node != NULL; tmp = node, node = node->nextAddress, free(tmp))
{
fprintf(fp, &quot;%s &quot;, node -> word);
printf(&quot;\n.........%s&quot;, node -> word);
}
fclose(fp);
return NULL;
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top