Thread: C ASK : I want to find all postion of substring in file return colum and line of file

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    1

    Post C ASK : I want to find all postion of substring in file return colum and line of file

    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;
    
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Deleting line 50, and putting this in place of lines 38-39 will get you first occurance on each line, but not all:
    Code:
    indexOf(fPtr, newWord, &line, &col);
    while (line != -1) {
      printf("line: %d, col: %d\n", line+1, col + 1);
      indexOf(fPtr, newWord, &line, &col); }
    Last edited by christophergray; 06-10-2018 at 10:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 04-06-2015, 09:36 PM
  2. Move file postion indicator to next line
    By arortell in forum C Programming
    Replies: 3
    Last Post: 07-28-2014, 04:09 PM
  3. Replies: 9
    Last Post: 10-30-2013, 11:23 PM
  4. How to find specific line in a text file?
    By leinad0213 in forum C Programming
    Replies: 3
    Last Post: 01-18-2012, 02:37 AM
  5. fgeting through a file to find a certain line
    By cnewbie1 in forum C Programming
    Replies: 2
    Last Post: 05-28-2011, 02:26 PM

Tags for this Thread