0

I am searching a string inside the the content of a file and storing the whole content in a buff char[] by excluding the space ' ' and at last comparing the this buff char[] with user input string for checking the availability.

But I am unable to store the whole file content because fgetc() is checking the space in if condition and placing to the next char even though I tried to use fseek() for pointing to the 1 char backward from the current position; it is making my program to terminate.

Please help me; my code follows:

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

FILE *file;
int file_size;

int main(void) {

    file= fopen("C:/Users/home/Desktop/dummy/s.txt","r");

    file_exist(file);

    fclose(file);
    return 0;
}

void file_exist(file)
{
    if(file)
    {
        printf("file exists\n");
        content_exist(file);
    }
    else
    {
        printf("it doesnt exist\n");
    }
}

void content_exist(file)
{
    fseek(file,0,SEEK_END);
    file_size=ftell(file);
    rewind(file);
    char user_input[10];
    if(file_size==0)
    {
        printf("content does not exists\n");
    }
    else
    {
        printf("content exist\n");
        printf("enter the word needs to be matched\n");
        scanf("%s",&user_input);
        check_string(user_input);
    }
}

void check_string(user_input)
{
    char buff[file_size];
    int temp=0;

    while(!feof(file))
    {
        printf("hi\n");
        if(fgetc(file)!=' ')
        {
            fseek(file, -1,SEEK_CUR);
            buff[temp]= fgetc(file);
            temp++;
        }
    }
    if(strcmp(user_input,buff)==0)
    {
        printf("your content matched\n");
    }
    else
    {
        printf("your content not matched\n");
    }
}
Dan D.
  • 73,243
  • 15
  • 104
  • 123
sumanth
  • 15
  • 7
  • 1
    Why not just keep the character read in the first `fgetc()` (in the `if` condition) for use in the `if` body, rather than trying to back up and reread the same character again? – Dmitri Nov 16 '14 at 07:07
  • Your posted code is not compilable. Can you please post code that can be successfully compiled? – R Sahu Nov 16 '14 at 07:12
  • how can i check the condition and at the same time storing that char in a array at the same time without using backup – sumanth Nov 16 '14 at 07:12
  • if i comment fseek(file, -1,SEEK_CUR); it is compilable – sumanth Nov 16 '14 at 07:22
  • 1
    Declare `int ch;` in the function, then change your `if` condition to get `if ((ch = fgetc(file)) != ' ')`. Get rid of the `fseek()`, and use `buff[temp]= ch;` in the `if` body. – Dmitri Nov 16 '14 at 07:23
  • Your code misses to prototype all functions defined (but `main()`). – alk Nov 16 '14 at 08:49
  • How would you think the end of the file is detected? – alk Nov 16 '14 at 09:11
  • i used feof() in while loop. which i believe would detect the end of the file. – sumanth Nov 16 '14 at 10:47
  • 1
    See [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) for reasons why your `feof()` test is wrong. I have used `feof()` a few times in my career; I went counting a few years ago and came up with about less than 10 places (out of thousands of source files). None of them was in a loop condition because it simply doesn't work properly. – Jonathan Leffler Nov 16 '14 at 15:53
  • this line: char buff[file_size]; could be a huge buffer, Better to use malloc() (and then free at the end of the code) as the stack is relatively limited in size. – user3629249 Nov 17 '14 at 01:16
  • this line: scanf("%s",&user_input); has a few problems. 1) the format string should have a leading ' ' so white space will be skipped. 2) the returned value from the call to scanf() (currently discarded) should be checked to assure that the user_input was properly set. 3) the %s should have a max size modifier, so the array 'user_input' is not overrun – user3629249 Nov 17 '14 at 01:20
  • this line: rewind(file); would be better written (to keep with the fseek theme) as fseek( file, 0, SEEK_SET ); Also, the returned value should be checked on all calls to fseek() to assure successful operation. returned value = 0 means success, all other values = failure – user3629249 Nov 17 '14 at 01:25
  • the fgetc() function returns an int. You will need to read the character into an int to be able to check for EOF – user3629249 Nov 17 '14 at 01:29
  • for good programing practice: in function content_exist() the scanf() for a string to be matched has nothing to do with the function, so should be in its' own function – user3629249 Nov 17 '14 at 01:32

2 Answers2

0

Fseek with integer values like -1 only work on binary files. Source

Try fopen with "rb" instead of just "r"

Ben
  • 2,867
  • 2
  • 22
  • 32
0

For your purpose, there doesn't seem to be any reason to use fseek.

Change this:

if (fgetc(file) != ' ')
{
    fseek(file,-1,SEEK_CUR);
    buff[temp] = fgetc(file);
    temp++;
}

To this:

    buff[temp] = fgetc(file);
    if (buff[temp] != ' ')
        temp++;

And of course, in order to use strcmp safely, you must terminate buff with a null-character:

buff[temp] = 0;
if (strcmp(user_input,buff) == 0)
...

Hence, please note that for a file with no space characters you will need char buff[file_size+1].

barak manos
  • 29,648
  • 10
  • 62
  • 114