Link to home
Start Free TrialLog in
Avatar of sudarshantk
sudarshantkFlag for India

asked on

decrement file pointer by 1

Dear Sir,
I am trying to dcrement a file pointer by 1 ie fseek(fp1, -1, SEEK_CUR)
It does not work correctly. Could you please let me know the correct implementation to decrement file pointers.
Rgds,
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image


I've never had a problem with fseek().

How about posting your code and giving us a better description of what you're seeing.

One thing to remember is that if you're trying to move the file pointer back so that it's positioned before the new-line character, you have to back up TWO characters for Windows/DOS files.


Kent

Avatar of sudarshantk

ASKER

Hi Kdo,
I am counting number of lines in a text file. But if there are comments in the line starting with "#" I need to ignore them.
Here is the code, please let me know. The code freezes at fseek...
Rgds,

int countlogs(FILE *fp1)
{
      int count;
        char buff[256];
        long int offset=-2;
        char ch;
        while(!feof(fp1))
        {
               ch=fgetc(fp1);

               if(ch=='#')
                    fgets(buff, 256, fp1);  
                else
                    fseek(fp1, offset, SEEK_CUR);              
              fgets(buff, 256, fp1);
                count++;
        }
        return count;
}

The fseek() is probably working just fine.  Looking at the code you're going to increment count whether the line starts with '#' or not.

Try this for you loop:

  while (fgets (buff, 256, fp1))
    if (*buff != '#')
      ++count;



Kent
Hi Kent,
fgets gets a line...
Only if the first char in the line is '#' I need to ignore the line. So we need to call function fgetc.
All the  other lines I need to count.
Actually the code is freezing after it runs fseek.
Rgds,

Yep.

Think about what your loop is doing:

        while(!feof(fp1))
        {
               ch=fgetc(fp1);

               if(ch=='#')
                    fgets(buff, 256, fp1);  
                else
                    fseek(fp1, offset, SEEK_CUR);              
             fgets(buff, 256, fp1);
                count++;
        }


At the start of a line you read a character.  If the character is a '#', indicating a comment, you set the file pointer back before the '#' character and continue.  When you next read a character, you read the same '#' again and reset the file pointer again.  You've created the infinite loop.  :)

That's why I suggested the short loop in my previous post.  It reads an entire line, and if the line is not a comment the counter is incremented.


Kent

ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
#include <stdio.h>
int main()
{
      int count=0;
        char buff[256];
        long int offset=-2;
        char ch;
        FILE *fp1;
        fp1=fopen("logtab1", "r");
       
        while(!feof(fp1))
        {
               ch=fgetc(fp1);

               if(ch=='#')
                    fgets(buff, 256, fp1);  
               else
               {
                    fgets(buff, 256, fp1);
                    count++;
               }
        }      
        fclose(fp1);
        printf("COUNT=%d\n", count);
        return count;
}


Logtab file
----------
#this is a test
TESTEMERG      1      Emergency test message
TESTALERT      2      Alert test message
TESTALERT      3      Alert test message
TESTALERT      4      Alert test message


Here count prints 5. It should be four. Could you please let me know why....
Rgds,

Sure.  After the line "ch=fgetc(fp1);" insert the following line:

  printf ("count = %d.  Checking %c\n", count, ch);


I think that should show you what's happening.  :)

Kent