1

I am reading a text file in C. I need to find the first occurrence of a particular character ('(') from the current pointer position and move the pointer to that place. I do this by reading into a buffer and then using strchr to find the character. Then I seek to the position of the character. The issue is that if I read till the end of file into my buffer, it will set the EOF indicator. Even though I seek back to the position of the character, the EOF indicator is set anyway and will mess with the error handling in the rest of the code. So how can I ensure that when I seek back to within the file's contents, EOF is set to false?

int io_read_until_char(char stopchar, FILE * ifile)
{
    char row[MAXCHAR];
    int offset, read_len;
    char * loc = NULL;

    while (!feof(ifile))
    {
        if(fgets(row, MAXCHAR, ifile) == NULL)
        {
            strerror(errno);
            break;
        }
        read_len = strlen(row);
        loc = strchr(row, stopchar);
        if(loc == NULL)
            continue;
        else
        {
            offset = -read_len + (int)(loc - row);
            fseek(ifile, offset, SEEK_CUR);
            break;
        }
    }
    if(feof(ifile) && loc == NULL)
    {
        return(-1);
    }
    else if(ferror(ifile))
    {
        perror("Error in parsing stuff!\n");
        strerror(errno);
        return(-1);
    }
    else
        return(0);
}

stark
  • 12,615
  • 3
  • 33
  • 50
adch99
  • 340
  • 1
  • 7
  • @AndreasWenzel, my code is for a text file containing a large matrix which got written by this very program a while back. There is no other program interacting with it. Therefore, it should be fine to use this here. I don't need to have a lot of security considerations because this code is for scientific calculations. – adch99 Dec 26 '22 at 09:07
  • Is your previous comment referring to my answer or to the comment that I deleted? If it is the former, then I suggest that you post that comment as a comment to my answer, instead of a comment to your question. – Andreas Wenzel Dec 26 '22 at 09:11
  • It was in reference to the comment you deleted. – adch99 Dec 26 '22 at 09:12
  • In my deleted comment, I referenced this: [Why is “while( !feof(file) )” always wrong?](https://stackoverflow.com/q/5431941/12149471). The reason why I retracted that comment was because I noticed that using this loop condition was harmless, in this case. However, it would make more sense to move the `fgets` call into the loop condition, like this: `while ( fgets(row, MAXCHAR, ifile) != NULL )` – Andreas Wenzel Dec 26 '22 at 09:16
  • 1
    Note that the line `strerror(errno);` does not do anything. Did you maybe mean `fprintf( stderr, strerror(errno) );` In that case, you may want to consider using [`perror`](https://en.cppreference.com/w/c/io/perror) instead. – Andreas Wenzel Dec 26 '22 at 09:18

1 Answers1

1

To clear the end-of-file indicator of a stream, you can call the function clearerr.

However, doing this should not be necessary. When calling the function fseek, the end-of-file indicator will be automatically cleared, assuming that the function call is successful.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39