fseek on different editors

This is a discussion on fseek on different editors within the C Programming forums, part of the General Programming Boards category; Hi, I am designing a parser in which I am using fseek to go back in a file. The problem ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    3

    fseek on different editors

    Hi,
    I am designing a parser in which I am using fseek to go back in a file.
    The problem that I am facing is that if the file opened is written in a notepad, then it works file. But if the file is written in some other editor,on using fseek, the file pointer moves to some other unintended position and causes the logic to fail.
    (It works fine on a single character but fails when I pass string length as a parameter))

    eg.
    long int vl_strlen = strlen("abcd");

    fseek(fp,vl_strlen,SEEK_CUR);

    Is there any solution to this problem ?

  2. #2
    Registered User
    Join Date
    Jun 2010
    Posts
    27
    fp must point to the undefined area.I suggest you use the whence "SEEK_SET" which is easy to control.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    3
    I am parsing a file with 1000 odd lines.
    If I have to to go back lets say, one word back from the current position, I can't use SEEK_SET in this case as it starts from the beginning of the file.
    And also it happens in between, so there is no chance of pointing to an undefined area..

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    27
    For what popurse you use the fseek(), you want to return the location or the number of characters?

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    For streams opened in text mode, fseek has limited use, because carriage return–linefeed translations can cause fseek to produce unexpected results. The only fseek operations guaranteed to work on streams opened in text mode are:
    From MSDN.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,807
    Good catch, Bayint Naung

    Yeah, we don't know how in which mode the file was opened. Perhaps the Ctrl-Z's occurring in files other than simple text files are interpreted as end-of-file indicators.

    Perhaps the text file came from another system where the end-of-line characters either include or don't include LF. Or is it CR. It's been so long ago. That's always been a transportability issue.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. You can't store EOF in a char. The reason it isn't reliable on "text mode" (which may not actually be different than "binary mode") is the NL / CR issue. Some use one, some use the other, some use both.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Can't you simply 'look ahead' say one token?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Bayint Naung View Post
    Can't you simply 'look ahead' say one token?
    fgetc + ungetc


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jun 2010
    Posts
    3
    Thanks guys...
    the problem was of CR LF only...
    opening the file in binary mode has solved the issue...

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Quote Originally Posted by quzah View Post
    fgetc + ungetc


    Quzah.
    Not so sophisticated. Only 1 char push back ?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes, you can only unget one character. I'm too tired to put it in the right words, but you're only ever allowed to unget the latest thing. If you've read two items, only the last one can be put back, and once you've used unget, you can't use it again without first reading something from the stream. (I.e., you can only unget the last thing read.)

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fseek failing - Error: Invalid argument
    By avi2886 in forum C Programming
    Replies: 14
    Last Post: 05-14-2009, 08:49 PM
  2. fseek question
    By blondie.365 in forum C Programming
    Replies: 19
    Last Post: 10-20-2008, 04:30 PM
  3. fseek() changing an unrelated variable?
    By aaronvegh in forum C Programming
    Replies: 3
    Last Post: 11-21-2007, 01:30 PM
  4. Help With fseek();
    By Explicit in forum C Programming
    Replies: 3
    Last Post: 05-26-2004, 08:40 PM
  5. popen and fseek
    By mach5 in forum C Programming
    Replies: 4
    Last Post: 11-29-2003, 01:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21