Thread: lseek error

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    31

    lseek error

    I'm just learning the unix system calls and have used lseek a few times and never had a problem with it. It's a pretty straight forward system call but this one is getting me. I'm getting the error "problem with lseek in lchars: 1 : Illegal seek"
    I've used the system call in this exact way but am getting this error for one reason or another. Probably overlooking something.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <ctype.h>
    
    int
    main(int argc, char **argv)
    {
            int fd1, fd2;
            int i;
            char buf[1];
    
            if (fd1 = open(argv[1], O_RDONLY) == -1) {
                    perror("problem with open in lchars: 1");
                    exit(-1);
            }
            if (fd2 = open(argv[2], O_CREAT|O_WRONLY, 0664) == -1) {
                    perror("problem with open in lchars: 2");
                    exit(-1);
            }
    
            i = 1;
            if (lseek(fd1, (off_t)-i, SEEK_END) == -1) {
                    perror("problem with lseek in lchars: 1");
                    exit(-1);
            }
    
            /* strip spaces at end of file */
            read(fd1, buf, sizeof(buf));
            while (isspace(buf[i])) {
                    ++i;
                    lseek(fd1, (off_t)-i, SEEK_END);
                    read(fd1, buf, sizeof(buf));
            }
    
            for (i; i < (i + 10); ++i) {
                    write(fd2, buf, sizeof(buf));
                    lseek(fd1, (off_t)-i, SEEK_END);
                    read(fd1, buf, sizeof(buf));
            }
    
            return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Are you trying to seek in an empty file?
    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.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    And what is the command you are using to invoke your program?

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    31
    no, I was sure the file wasn't empty and tried it on a couple files. the command was
    programname /etc/passwd file.txt
    programname program.c file.txt
    Both have read permissions for the user.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    Quote Originally Posted by crash88
    Code:
            if (fd1 = open(argv[1], O_RDONLY) == -1) {
                    perror("problem with open in lchars: 1");
                    exit(-1);
            }
            if (fd2 = open(argv[2], O_CREAT|O_WRONLY, 0664) == -1) {
                    perror("problem with open in lchars: 2");
                    exit(-1);
            }
    Try changing this to:

    Code:
            if ((fd1 = open(argv[1], O_RDONLY)) == -1) {
                    perror("problem with open in lchars: 1");
                    exit(-1);
            }
            if ((fd2 = open(argv[2], O_CREAT|O_WRONLY, 0664)) == -1) {
                    perror("problem with open in lchars: 2");
                    exit(-1);
            }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM