I am having some problems on this one.
I want to read a text and everytime that it reads the word "test four", I want to replace it with the word "view five".
I came up with this code, using the FAQ on this site. Anyone know what I do terribly wrong?
Code:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#define MAX_LINE (128+1) /* Maximum length of a source line plus one */
int main()
{
char InputBuffer[MAX_LINE];
FILE *Infile;
char *string[MAX_LINE];
if ((Infile = fopen("input.txt", "r")) == NULL) {
fprintf(stderr, "Error opening input-file :%s\n", "input.txt");
exit(1);
}
while (!feof(Infile)) {
(fgets(InputBuffer, MAX_LINE, Infile));
if ((string = strstr(InputBuffer, "test four")) !=NULL)
*string = "view five";
printf( "%s\n",InputBuffer);
}
return(0);
fclose (Infile);
}
I used strstr.. because K&R says that it is the same as strchr, except that it is not for characters, but for strings.