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("\nEnter the File name and two strings...\n"
exit(0);
}
if( (fp = fopen(a[1], "r") == NULL)
{
perror("Unable to Open the file"
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, "%s", 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("result.txt", "w"
for(tmp = node; node != NULL; tmp = node, node = node->nextAddress, free(tmp))
{
fprintf(fp, "%s ", node -> word);
printf("\n.........%s", node -> word);
}
fclose(fp);
return NULL;
}
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("\nEnter the File name and two strings...\n"
exit(0);
}
if( (fp = fopen(a[1], "r") == NULL)
{
perror("Unable to Open the file"
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, "%s", 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("result.txt", "w"
for(tmp = node; node != NULL; tmp = node, node = node->nextAddress, free(tmp))
{
fprintf(fp, "%s ", node -> word);
printf("\n.........%s", node -> word);
}
fclose(fp);
return NULL;
}