I am trying to search a file (I have searched the forums and have found some treads but they do not really help me to do what I want) directly using fseek and strcmp but its buggy and gives off by one errors and such. The reason I did not want to store in an array because the text it will be extracting from is a log file and it will increase over time, but if anyone has any suggestions of any other way then they are free to give it.

1. The program is to find a string and then position the curcer/pointer (not sure of the definition here) at the start of it in so I can put it in my other code to do some (fscanf/fgets).
2. The program dosn't have to printout the position (I just did that so I could know how many spaces to count off.
3. (This is to be included in my main code as a fuction to read .csv files)
I hope you will not misinterpret this as a list of demands, I just did that so the program would be clearer.:-O

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/********************************************************************
Program to find and replace in a file
It also tells the user where the  file curser is
Shane Miller with assistance from Koy.
*********************************************************************/

int main () {
    char string_to_replace[6],file_char_to_replace[6];
    int lFileLen,i; char tmp[6]; int pos_of_file_curcer=0;
    FILE * tstPtr;

    printf("Please enter Number to replace\n");// then tmp for first and last name
    scanf("%s",string_to_replace);

    tstPtr=fopen("tmp.txt","r+");
    fseek(tstPtr,0L,SEEK_END);    /* Position to end of file */
    lFileLen = ftell(tstPtr);        /* Get file length */
    rewind(tstPtr);                  /* Back to start of file */
    printf (" File Lenght is %d\n",lFileLen);

    for(i=0;i<=lFileLen;i++){

        fscanf(tstPtr,"%s\n",tmp);

        if(!strcmp(tmp,string_to_replace)){
            printf ("number found\n");
            //pos_of_file_curcer = i;
            pos_of_file_curcer = ftell( tstPtr );
            pos_of_file_curcer -=6;
            printf (" Posistion of number is %d\n",pos_of_file_curcer);
            fseek ( tstPtr , pos_of_file_curcer/*ftell ((tstPtr)-4)*/ , SEEK_SET );
            printf ("Enter string to replace it with\n");
            scanf ("%6s",file_char_to_replace);
            fputs ( file_char_to_replace , tstPtr );


            fclose(tstPtr);
            break;
        }
        if (strcmp(tmp,string_to_replace)) {
            printf ("number not found\n");
            printf ("i is %d\n",i);

        }



    }
    system ("pause");
    return 0;
}

Recommended Answers

All 2 Replies

First, I do not understand the sacral meaning of a number 6, especially at line 33. Maybe you know something about how the file is arranged, but in any case I would not rely on magic numbers.

In your situation I'd get rid of line 33, and moved line 32 to 26.

Never use scanf() to read a string (here's why). In your code, if you enter more than 5 characters you are overwriting memory you shouldn't touch.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.