Solved

decrement file pointer by 1

Posted on 2006-06-15
8
1,262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
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:Kent Olsen
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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:Kent Olsen
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:
Kent Olsen 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:Kent Olsen
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adjust Mfcapp 29 178
Coverting 24 hour time to 12 hour in C++ 15 182
how to understand recursion 12 233
delete-remove 14 108
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

730 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