I'm learning about file commands and trying to write some programs. I want to specify two characters in command line, where the second one will replace the first one, every time the first character is found. The command line input would be [program_name].exe [file].txt [old_char] [new_char]. I came across this thread which had the same problem and I tried to fix my code by looking at the answer, but it creates an infinite loop.
int main(int argc, char *argv[])
{
FILE *fp;
char *string = {"Hellx"};
char c;
if ((fp = fopen(argv[1], "w")) != NULL)
{
fputs(string, fp);
printf("Successfully written.\n");
fclose(fp);
if ((fp = fopen(argv[1], "r+")) != NULL)
{
while ((c = fgetc(fp)) != EOF)
{
if (c == *argv[2])
{
fseek(fp, ftell(fp) - 1, SEEK_SET);
fprintf(fp, "%c", *argv[3]);
printf("Successfully changed.\n");
}
}
fclose(fp);
}
else
printf("Error on opening!");
}
else
printf("Error on writing!");
return 0;
}
So the output for this would be: Helloelloelloelloelloelloello..., while it should just change x to o. What's the problem with this code?