Thread: Illegal seek error

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    52

    Exclamation Illegal seek error

    Hey all,

    Can anyone help me figure out why I'm getting the following error: "fseek in do_this:Illegal seek"?

    Code:
    void do_this(int file)
    {
    	char c;
    	int disp = 0; //number of char read during this call
    	int lines_read = 0; //number of lines read during this call
    	if (lseek(file, pos, SEEK_SET) == -1);
    	{
    		int errNum = errno;
    		display("lseek in do_this:");
    		disp_err_msg(errNum);
    	}
    		while((pos <= file_size) && (pos > -1) && (lines_read <20))
    		{
    			if(read(file, &c, 1) == -1)
    			{
    				int errNum = errno;
    				display("read in do_this:");
    				disp_err_msg(errNum);
    			}
    			if (c == '\n')
    			lines_read++;
    			pos++;
    			disp++;
    			if(write(1, &c, 1) == -1)
    			{
    				int errNum = errno;
    				display("write in do_this:");
    				disp_err_msg(errNum);
    			}
    		}
    }
    Of course, pos is a global variable...

    Thanks!
    Last edited by Canadian0469; 10-30-2007 at 03:52 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is the value of pos, and how big is the file?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    "fseek in do_this:Illegal seek"
    Do you not mean "lseek in do_this: Illegal seek"?

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    Tommo: Yes I was looking at an old printout sorry.
    matsp: pos is initially 0 and I'm trying different files in my home directory.

    Thanks for all the replies so far. Any more help would be very appreciated too

    Judging from the error I thought it had to do with the fact that I was running my program over SSH but then I tried running the code locally in Ubuntu and it still didn't work...

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note: Avoid global variables.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    I need to use global variables as a requirement for my assignment...

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, 0 is not an invalid seek location, but if you get an error, then it's most likely because you are trying to seek to something that is non-zero, most likely the case is that you are trying to seek past the end of the file.

    Print pos when you get the error, and which file it is for.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    I tried replacing pos with the value 0. Nothing changes...
    The file is a basic 100 byte c program (one of several files I've tried).

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. What number is "errno"? [ideally, I'd like to know the symbolic name, e.g. EINVAL, EBADFD or such].
    2. Are you compiling the code with the correct header files, and does compiling with "-Wall" give you any warnings?

    I think the error you are getting is this:
    [EINVAL] Whence is not a proper value.
    If the prototype for lseek() is not matched with the actual call, then "whence" is a random value on the stack [the second parameter offset is a 64-bit integer, and if you don't have the correct include file, the call would make it three 32-bit integers, causing the third "whence" parameter to be something other than the expected value, since two of the 32-bit values are "eaten" as the offset].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    Thanks for the reply again,
    I'm still looking into what you mentioned. I did find the errno to be 29 though...

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is ESPIPE, which says that you are trying to do seek on a pipe. What does your code that calls "do_this" actually look like? Are you reading from a pipe or some such?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    It's a pretty huge program that's the problem... I open a file whose name is passed as an argument and I save the file descriptor number in "file".

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, the error code 29 means ESPIPE, which is said on lseek to be given when:
    [ESPIPE] Fildes is associated with a pipe, socket, or FIFO.
    http://www.hmug.org/man/2/lseek.php

    Do you actually need to perform a seek at all - if you just opened the file, it should be at the beginning of the file [unless you opened it in append mode].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    Hey thanks for all the help mats (and others) turns out it was this portion of the code that messed me up:

    Code:
    if (lseek(file, pos, SEEK_SET) == -1);
    	{
    		int errNum = errno;
    		display("lseek in do_this:");
    		disp_err_msg(errNum);
    	}
    I can't explain it but when I removed the error checking and just performed lseek it works... It seems like lseek actually returns -1 even though notihng is wrong...

    I'm lost but it works

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I have a feeling it "works" because your lseek isn't actually doing anything. Put a
    Code:
    if (pos != 0) printf("pos = &#37;d\n", pos);
    before your lseek, and see if that fires ever.

    Edit: I perhaps should explain what I mean: If you have just opened a file, then the file would be at position zero, so if you try to lseek() unsuccessfully, to position zero, then you end up at position zero anyways. If you were to try to seek to another position, then it would be a different matter, and lseek() would make a difference.

    --
    Mats
    Last edited by matsp; 10-30-2007 at 07:22 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM