fseek() Question

This is a discussion on fseek() Question within the C Programming forums, part of the General Programming Boards category; Hi Guys, fseek() return 0 on succses and nonZero when failed. For example the following code returns -1 Code: FILE ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    84

    fseek() Question

    Hi Guys,

    fseek() return 0 on succses and nonZero when failed.

    For example the following code returns -1
    Code:
    FILE *fpRead;
    int iResult;
    
    fpRead=fopen("textfile.txt","rt");
    if (fpRead==NULL)
    {
        puts("File not found!");
        exit(1);
    }
    
    iResult=fseek(fpRead,-10,SEEK_SET);
    This make sense because I am trying to move backward from the beginning of the file.

    My question is, Why is the following code don't return the same error and show succses??
    Code:
    FILE *fpRead;
    int iResult;
    
    fpRead=fopen("textfile.txt","rt");
    if (fpRead==NULL)
    {
        puts("File not found!");
        exit(1);
    }
    
    iResult=fseek(fpRead,100000L,SEEK_END);
    My text file is only 340 bytes and still this code progress 100,000 bytes, How can it be?

    Thank you !!!

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    What's the "rt" mode?

    Anyway... SEEK_END doesn't seek backward, it just sets the relative position. You are seeking FORWARD in the file, which is perfectly fine to do.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,659
    rt = read, text
    Anyway, fseek seeks in the file relative to what you specify. Since SEEK_SET is fixed at the beginning of the file, and you try to seek a negative number, you're going to seek... before the file? Doesn't make sense, does it?
    The second example with SEEK_END is also poor, because it tries to seek beyond the file, but I'm guessing fseek just sets it to the end of the file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by Elysia View Post
    rt = read, text
    Code:
    Mode  	Meaning
    "r" 	Open a text file for reading
    "w" 	Create a text file for writing
    "a" 	Append to a text file
    "rb" 	Open a binary file for reading
    "wb" 	Create a binary file for writing
    "ab" 	Append to a binary file
    "r+" 	Open a text file for read/write
    "w+" 	Create a text file for read/write
    "a+" 	Open a text file for read/write
    "rb+" 	Open a binary file for read/write
    "wb+" 	Create a binary file for read/write
    "ab+" 	Open a binary file for read/write
    Didn't know there was another extra string mode.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,659
    t
    Open in text (translated) mode. In this mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing with "a+", fopen checks for a CTRL+Z at the end of the file and removes it, if possible. This is done because using fseek and ftell to move within a file that ends with a CTRL+Z, may cause fseek to behave improperly near the end of the file.

    Also, in text mode, carriage return–linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return–linefeed combinations on output. When a Unicode stream-I/O function operates in text mode (the default), the source or destination stream is assumed to be a sequence of multibyte characters. Therefore, the Unicode stream-input functions convert multibyte characters to wide characters (as if by a call to the mbtowc function). For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters (as if by a call to the wctomb function).
    b
    Open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed.

    If t or b is not given in mode, the default translation mode is defined by the global variable _fmode. If t or b is prefixed to the argument, the function fails and returns NULL.

    For more information about using text and binary modes in Unicode and multibyte stream-I/O, see Text and Binary Mode File I/O and Unicode Stream I/O in Text and Binary Modes.
    From msdn's docs.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by Elysia View Post
    rt = read, text
    Anyway, fseek seeks in the file relative to what you specify. Since SEEK_SET is fixed at the beginning of the file, and you try to seek a negative number, you're going to seek... before the file? Doesn't make sense, does it?
    The second example with SEEK_END is also poor, because it tries to seek beyond the file, but I'm guessing fseek just sets it to the end of the file.
    You can seek beyond the current end of file, this is a perfectly well-defined operation. This creates a 1 megabyte file consisting of zero bytes:

    Code:
    FILE *fp = fopen(filename, "w");
    fseek(fp, 1024*1024-1, SEEK_SET);
    fputc(0, fp);
    fclose(fp);

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by Aia View Post
    Didn't know there was another extra string mode.
    There isn't, it's some non-standard Microsoft thing.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    >My question is, Why is the following code don't return the same error and show succses??
    >You can seek beyond the current end of file, this is a perfectly well-defined operation. This creates a 1 megabyte file consisting of zero bytes:
    So As brewbuck said that the FILE pointer can be seeked beyond file size which is completly valid .And hence you could get any error message.

    ssharish

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    Quote Originally Posted by Elysia View Post
    The second example with SEEK_END is also poor, because it tries to seek beyond the file, but I'm guessing fseek just sets it to the end of the file.
    That's the thing!, fseek DOESN'T point to the end of the file.
    After running the following code:
    Code:
    fpRead=fopen(cFilename,"r");
    	if (fpRead==NULL)
    	{
    		puts("File not found!");
    		exit(1);
    	}
    
    	if (fseek(fpRead,100000L,SEEK_END))
    	{
    		puts("Error skipping file");
    		exit(1);
    	}
    
    
    	printf("%ld\n",ftell(fpRead));
    The result is: 100612, even that my file length is 612 bytes.
    So were am I actully pointing?

    Thanks again

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    > So were am I actully pointing?
    Past the end of the file of course. You asked for 100000 bytes past the end of the file, and that's where you got to.

    However, what happens next really depends on
    a) how you opened the file.
    b) whether you try to read or write to the file.

    Look at brewbuck's example in post #7

    If you really want the end, then it's
    fseek(fpRead,0,SEEK_END)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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() changing an unrelated variable?
    By aaronvegh in forum C Programming
    Replies: 3
    Last Post: 11-21-2007, 01:30 PM
  3. fseek high cpu usage?
    By chunlee in forum C Programming
    Replies: 2
    Last Post: 02-19-2005, 06:27 AM
  4. popen and fseek
    By mach5 in forum C Programming
    Replies: 4
    Last Post: 11-29-2003, 01:03 AM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM

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