Solved

decrement file pointer by 1

Posted on 2006-06-15
8
1,238 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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
 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

786 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