0

I'm quite new to C. I'm trying to write a code that finds a string in a I/O stream, and I don't understand what I'm doing wrong. I know the error is probably in the large while loop (in the code below). I want the function to return the location in bytes from the beginning of the stream and -1 if it fails for some reason. It just keeps returning -1 for any file I try it on.

long find_string(const char *str, const char *filename, long offset)
{
FILE *f = fopen(filename, "r");
if (!f){
    return -1;
}

int s=0,c;

c = fgetc(f);
if(c == EOF){
    return -1;
}

char *check = malloc(sizeof(char));

fseek(f, 0L, SEEK_END); // Sees and stores how long the file is
long sz = ftell(f);
fseek(f, 0L, SEEK_SET);

if(fseek(f, offset,SEEK_SET) != 0){ // finds the position of offset
    return -1;
}


while(fgetc(f) != EOF){
    c = fgetc(f);
    if(c == str[0] && ftell(f) < sz){
        check[0] = c;
        offset = ftell(f);
        }

        s++;
        for (unsigned int r=1; r < (strlen(str));r++){
            c = fgetc(f);
            if(c == str[s]){
                    check = realloc(check, sizeof(char)*s);
                    check[s] = c;
                    s++;
            }                   
        }

    if(strcmp(check, str)==0){
        free(check);
        fclose(f);
        break;

    }
    else{
        check = realloc(check, sizeof(char));
        offset = -1;
    }
}
return offset;}  

Any help is greatly appreciated

jilin
  • 3
  • 1
  • Ouch. I don't know what your specific problem is but you have made this 3 times harder than it has to be. Consider rethinking the whole thing. – Duck Apr 22 '14 at 12:24
  • 1
    In order to teach you how to fish, rather than returning -1 for all errors, you could return different values. That at least will tell you what section of the function is failing (and would also help us) – Peter M Apr 22 '14 at 12:26
  • possible duplicate of [C: searching for a string in a file](http://stackoverflow.com/questions/2188914/c-searching-for-a-string-in-a-file) – John Zwinck Apr 22 '14 at 12:30
  • "what I'm doing wrong" — basically just about everything. – n. m. could be an AI Apr 22 '14 at 12:31

2 Answers2

0

This would be much easier if you simply memory-mapped the entire file and ran a standard string searching algorithm on it.

For memory mapping, see: Linux - Memory Mapped File

For string searching code, see: strstr() for a string that is NOT null-terminated

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

Please check the lines with comment updated

long find_string(const char *str, const char *filename, long offset)
{
FILE *f = fopen(filename, "r");
if (!f){
    return -1;
}

int s=0,c;

c = fgetc(f);
if(c == EOF){
    return -1;
}

char *check = malloc(sizeof(char));

fseek(f, 0L, SEEK_END); // Sees and stores how long the file is
long sz = ftell(f);
fseek(f, 0L, SEEK_SET);

if(fseek(f, offset,SEEK_SET) != 0){ // finds the position of offset
    return -1;
}

    c = fgetc(f); // Updated
while(c != EOF){ // Updated
    if(c == str[0] && ftell(f) < sz){
        check[0] = c;
        offset = ftell(f);
        }

        s++;
        for (unsigned int r=1; r < (strlen(str));r++){
            c = fgetc(f);
            if(c == str[s]){
                    check = realloc(check, sizeof(char)*s);
                    check[s] = c;
                    s++;
            }                   
        }

    if(strcmp(check, str)==0){
        free(check);
        fclose(f);
        break;

    }
    else{
        check = realloc(check, sizeof(char));
        offset = -1;
    }
    c = fgetc(f); //Updated
}
return offset;}  

since you are using fgetc at the condition and start of the look, you actually comparing the second char of file with first char of str. update and check.

user207064
  • 665
  • 5
  • 14