it return 1 postion, but i want to it return all postion of substring. Can you help me. thank
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1000
#define MAX_SIZE 1000
int main()
{
/* File pointer to hold reference of input file */
FILE * fPtr;
FILE * fTemp;
char path[100];
char buffer[BUFFER_SIZE];
char oldWord[100], newWord[100];
int line, col,i;
printf("Enter file name: ");
scanf("%s", path);
fPtr = fopen(path, "r");
fTemp = fopen("replace.tmp", "w");
if (fPtr == NULL || fTemp == NULL)
{
/* Unable to open file hence exit */
printf("\nUnable to open.\n");
printf("Please check your file.\n");
exit(EXIT_SUCCESS);
}
else
{
printf("Input a word: ");
scanf("%s", newWord);
indexOf(fPtr, newWord, &line, &col);
if (line!= -1)
printf("line: %d, col: %d\n", line+1, col + 1);
fclose(fPtr);
}
}
int indexOf(FILE *fptr, const char *word, int *line, int *col)
{
char str[BUFFER_SIZE];
char *pos;
*line = -1;
*col = -1;
while ((fgets(str, BUFFER_SIZE, fptr)) != NULL)
{
*line += 1;
// Find first occurrence of word in str
pos = strstr(str, word);
if (pos != NULL)
{
// First index of word in str is
// Memory address of pos - memory
// address of str.
*col = (pos - str);
col++;
break;
}
}
// If word is not found then set line to -1
if (*col == -1)
*line = -1;
return *col;
}