Alright I wrote the program the way I thought it should work but I am getting some errors when using subStrBeg, subStrEnd and i together. I understand that they're different data types...but I don't know how exactly to resolve this problem.
I get these errors:
Code:
r.c:33: warning: comparison between pointer and integer
r.c:35: warning: passing argument 1 of ‘fputs’ makes pointer from integer without a cast
r.c:41: warning: assignment makes integer from pointer without a cast
r.c:43: warning: passing argument 1 of ‘fputs’ makes pointer from integer without a cast
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 512
#define _GNU_SOURCE
int main(int argc, char *argv[])
{
int i = 0;
FILE *file, *file2;
char line[MAX], *subStrBeg, *subStrEnd;
int subStrLen, strLength;
if (argc != 4)
{
printf("You must enter: ./replace old-string new-string file-name\n");
exit(1);
}
file = fopen(argv[3], "r");
file2 = fopen("temp", "w");
while (fgets(line,sizeof(line),file) != NULL)
{
/* Find when string begins, length of string before
substring, and then the end of the substring */
subStrBeg = strstr(line, argv[1]);
subStrLen = subStrBeg-line;
subStrEnd = subStrBeg+strlen(argv[1]);
strLength = strlen(line);
/* Write left part of string to file */
for (i=0; i < subStrBeg; i++)
{
fputs(line[i], file2);
}
/* Write replaced string to file */
fputs(argv[2], file2);
/*Write right part of string to file */
for (i=subStrEnd; i < strLength; i++)
{
fputs(line[i], file2);
}
}
/* Close everything */
fclose (file);
fclose (file2);
return 0;
}
If anyone can show me an example of how to fix this or a internet page that explains what to do in this situation...it would be great
Thanks,
Dan