Solved

decrement file pointer by 1

Posted on 2006-06-15
8
1,206 Views
Last Modified: 2008-01-09
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,
0
Comment
Question by:sudarshantk
  • 5
  • 3
8 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 16913640

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

0
 

Author Comment

by:sudarshantk
ID: 16913724
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;
}
0
 
LVL 45

Expert Comment

by:Kdo
ID: 16913745

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
0
 

Author Comment

by:sudarshantk
ID: 16913942
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,
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 45

Expert Comment

by:Kdo
ID: 16914220

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

0
 
LVL 45

Accepted Solution

by:
Kdo earned 125 total points
ID: 16914348

Sorry.  My description was lousy, but the problem is still in the loop.  :)

When the line is NOT a comment the file pointer is set back one character and the entir line is read.  However, you've just set the file pointer so that it's positioned immediately before the prior new-line character.  When you read the line you read only the new-line and repeat the loop, which will process the same line over again and again....


Kent

0
 

Author Comment

by:sudarshantk
ID: 16914381
#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,
0
 
LVL 45

Expert Comment

by:Kdo
ID: 16914450

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
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now