thank you... I try your method and I got an error saying "segmentation fault (core dumped)" This is what the revised code looks like:
Code:
#include<stdio.h>
#include<string.h>
int main(void)
{
#define STRSIZE 20
FILE *pf;
char str[STRSIZE]; /*Maximum size of a string is 20*/
char ustring[STRSIZE];
char *chpoint;
char *prvstring;
pf = fopen("input.txt","r");
if (!pf)
{
printf("Can't open the file\n");
return 0;
}
printf("Enter a string. ");
chpoint = strchr(ustring, '\n'); /*searches for "\n", address is returned if found; otherwise NULL is returned*/
if (chpoint != NULL)
{
*chpoint = 0; /*removes the \n*/
}
while (fscanf(pf, "%s",str) == 1) /*continue to scan and print strings until the EOF*/
{
int v = strcmp (ustring, str);
if (v == 0)
{
printf ("<%s> equals <%s>.\n", ustring, str);
}
printf("previous string is: %s \n", str);
strcpy(prvstring, str);
printf("new string is: %s\n", prvstring);
}
fclose(pf); /*close file*/
return 0;
}
Do you know why is it doing that?
Thanks