Search code examples
ctext-filesfgetc

Scrolling in text file in C


I have a problem with C language. I have to find and print every second word from input text file to output text file between words START and STOP. I make a function, which control if is next word in text file STOP. But if is in input text file word SToP, function fgetc move to T and check, that next letter is not O and break... But I need to get before word SToP. How can I do this? I cant use any library functions except for fgetc, fputc, fopen nad fclose.

Here is code:

int search_STOP(int ch, FILE* in) { if(ch == 'S'){
  ch = fgetc(in);
  if(ch =='T'){
    ch = fgetc(in);
    if(ch == 'O'){       
       ch = fgetc(in);
       if(ch == 'P'){       
             return 1;
       }
    }
  }
  return 0;
}

Solution

  • I faced the same problem once. Most of these problems you can solve by googling and stitching code snippets together.

    I changed the code I made back then a little for your purpose (since the job I was performing was slightly more complex than just ignoring case in the searchword). Note that it is important that you provide your search word ("start") in lower case in this piece of code. You could change it so that it also takes upper case search words but I'll leave that to you to figure out on your own.

    #include <stdio.h>
    #include <stdlib.h>
    
    int specialFind(char *buffer, const char *s, int startIndex, int endIndex);
    
    int specialFind(char *buffer, const char *s, int startIndex, int endIndex)
    {
        int findIndex = 0;
    
        int s_len = 0;
        int signComp = 0;
    
        while(s[s_len] != '\0')
        {
            s_len++;
        }
    
        char msg[64];
    
        for(int i = startIndex; i < endIndex; i++)
        {
            if(buffer[i] >= 65 && buffer[i] <= 90)
            {
                signComp = 32;
            }
            else
            {
                signComp = 0;
            }
            if(buffer[i] + signComp == s[findIndex])
            {
                findIndex++;
            }
            else
            {
                findIndex = 0;
            }
            if(findIndex == s_len)
            {
                return i - s_len + 1;
            }
        }
        return -1;
    }
    
    int main()
    {
        FILE *fileptr;
    
        //  First opening and reading to determine file size
        fileptr = fopen("textfile.txt", "r");
        int filelen = 0;
        char c;
        while(1)
        {
            c = fgetc(fileptr);
            filelen++;
            if(feof(fileptr))
            {
                break;
            }
        }
        fclose(fileptr);
    
        //  Now we're actually gonna read the file to the buffer
        char buffer[filelen];
        fileptr = fopen("textfile.txt", "r");
        int index = 0;
        while(1)
        {
            buffer[index] = fgetc(fileptr);
            index++;
            if(feof(fileptr))
            {
                break;
            }
        }
        fclose(fileptr);
    
        return 1;
    }