Home > OS >  Is there a way to setback ptr inside my file after using fgetc()?
Is there a way to setback ptr inside my file after using fgetc()?

Time:12-26

int main(){
    int ms = 0, vs = 0, cif = 0, intzn = 0, i;
    FILE* dat = fopen("file.txt", "r");
    for(i = 0; !feof(dat); i  )
    {
        if(isupper(fgetc(dat)))
            vs  ;
        else
        {
            fseek(dat, ftell(dat) - 1, SEEK_SET);
        }
        if(islower(fgetc(dat)))
            ms  ;
        else
        {
            fseek(dat, ftell(dat) - 1, SEEK_SET);
        }
        if(isdigit(fgetc(dat)))
            cif  ;
        else
        {
            fseek(dat, ftell(dat) - 1, SEEK_SET);
        }
        if(ispunct(fgetc(dat)))
            intzn  ;
    }
    printf("\nVS: %d\nMS: %d\nCif: %d\nIntznc: %d", vs, ms, cif, intzn);
    fclose(dat);
    return 0;
}

every time i use "fgetc(dat)" in my if statement, the pointer pointing to that character in that file advances, so I am trying to figure out how to set it back in case my if statement is false, where I've tried using "fseek()" but it still wont work, why?

CodePudding user response:

Your loop should not use feof to test for end-of-file because the end-of-file indicator is set only after the end is reached. It cannot tell you in advance there is not another character to get.

Looking at your loop, I suspect you do not need to “go back” in the file. You just need to read one character and examine it in multiple ways. To do that, you can simply read the character and assign its value to a variable. Then you can use the variable multiple times without rereading the character. For future reference, when you do want to reject a character, you can “put it back” into the stream with ungetc(c, dat);.

Further, I suspect you want to read each character in the file and characterize it. So you want a loop to read through the file until the end. To do this, you can use:

To read one character, test it, and reject it if it is not satisfactory, use:

while (1) do
{
    /*  Use int to get a character, because `fgetc` may
        return either an unsigned char value or EOF.
     */
    int c = fgetc(dat);

    //  If fgetc failed to get a character, leave the loop.
    if (c == EOF)
        break;

    //  Now test the character.
    if (isupper(c))
          vs;
    else if (islower(c))
          ms;
    else if (isdigit(c))
          cif;
    else if (ispunct(c))
          intzn;

    //  If desired, include a final else for all other cases:
    else
          other;
}

CodePudding user response:

There are several issues with your program, among them:

  • for (...; !feof(f); ...) is wrong. The feof() function determines whether end-of-file has been observed by a previous read from the file. If one has not, then that function does not speak to whether a future read will succeed. feof() is for distinguishing between read failures resulting from EOF and those resulting from I/O errors, not for predicting the future.

  • When one of your tests succeeds, you do not go back to the beginning of the loop. So suppose that your fseek()s were all working (which may in fact be the case), that your input is "StackOverflow", and that at the beginning of a loop iteration, the 'k' is the next character. Then

    • the 'k' will be read and rejected by the uppercase test, and the file moved back
    • the 'k' will be read an accepted by the lowercase test
    • the 'O' will be read and rejected by the digit test, and the file moved back
    • the 'O' will be read and rejected by the punctuation test, and lost because the file is not moved back.

I've tried using "fseek()" but it still wont work, why?

It's hard to tell for sure without the input you are using and an explanation of what behavior you actually observe, but likely the problem is related to the second point above.

The thing to do is probably to

  1. avoid any need to push back a character. On each iteration of the loop, read one character from the file and store it in a variable, then perform as many tests as you like on that variable's value.

  2. test the return value of fgetc() (instead of using feof()) to determine when the end of the file has been reached. This dovetails with (1) above.

  •  Tags:  
  • c
  • Related