lseek. Positioning cursor at end of file problem

This is a discussion on lseek. Positioning cursor at end of file problem within the C Programming forums, part of the General Programming Boards category; I'm trying to set the current position to a location well beyond the end of the file. However, when I ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    141

    lseek. Positioning cursor at end of file problem

    I'm trying to set the current position to a location well beyond the end of the file.

    However, when I input a string such as "testing 1 2 3.." at the end of the file with offset eg 1000 bytes, I can no longer open the file. It says

    Code:
    gedit has not been able to detect the character coding.
    Please check that you are not trying to open a binary file.
    Select a character coding from the menu and try again.
    Also, is there a command I can put inside my code that actually reads the file and displays it on the screen?
    Seems like nread just returns a ssize_t value.

    Code
    Code:
    #include <stdio.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    int main()
    
    {
    	int fd;	
    	ssize_t nread,nwrite,nbytes;
    	off_t newpos;
    	char buffer[1024];
    
    	/*open file for reading*/
    
    	fd = open("lseek.txt",O_RDWR);
    
    	if (fd == -1)
    	{
    		printf("Error! Could not open file\n");
    		exit(1);
    	}
    
    	newpos = lseek(fd,20000, SEEK_END);
    
    	nread = read(fd, buffer, 20);	
    
    	printf("End of file: %s\n", buffer);
    
    	
    	strcpy(buffer,"testing 1 2 3...");
    	nbytes = strlen(buffer);	
    	nwrite = write(fd,buffer,nbytes);
    
    	printf("End of file: %s\n", buffer);
    	
    	
    	close(fd);
    
    }
    Last edited by bos1234; 04-01-2012 at 02:34 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Well if you seek past the end of a file and then write something, then the gap you left will be read as \0

    This is not an ASCII character, so gedit will barf.

    I suggest using a hex editor if you want to see what the file looks like.
    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.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    141
    Quote Originally Posted by Salem View Post
    Well if you seek past the end of a file and then write something, then the gap you left will be read as \0

    This is not an ASCII character, so gedit will barf.

    I suggest using a hex editor if you want to see what the file looks like.
    thanks for your reply.

    Say I add a string ("hello" to the end of the file (eg. with offset 20000 bytes after EOF as above).

    If I type
    Code:
    cat filename.txt
    , it displays two lines of text

    Code:
    random text in here
    hello
    but if i type
    Code:
    more filename.txt
    it displays the first line of text and has the option of "more". When I keep pressing enter to the end of the file, the final string (hello) doesn't exist.

    Anyone know why this is the case?
    random text in here

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    As Salem has told you by seeking past the end of the file and then writing some more text you are creating a file that contains non printable NUL characters.
    It seems that cat just ignores this NULs and more just gets confused.
    After all you are using programs that are supposed to display text-files with a file that isn't a proper text-file. So anything can happen.
    The question is: Why do you want to create such a file in the first place ?
    Kurt

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You still need to open your file in a hex editor. If you don't have one (and are too lazy to google/install one), a function that does this is actually really easy to write. You can format the output however you want, but the gist of the function should resemble:

    Code:
    #include <fcntl.h>
    #include <unistd.h>
    
    /* ... */
    
    void hex_dump (const char *path)
    {
        int i, fd;
        char buf[9];
    
        fd = open(path, O_RDONLY);
        
        while (read(fd, buf, 8) > 0) {
            for (i = 0; i < 8; i++) 
                printf("%.02x ", buf[i]);
            printf("\n");
        }
    
        close(fd);
    }
    Or something, I didn't test it. Either way, you could try c/ping the function into your code and inputting your file, and see what it prints out. If it spits a bunch of "00 00 00" before valid characters, you need to fix your other code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. positioning the cursor(plz help)
    By rajatkochhar in forum C Programming
    Replies: 12
    Last Post: 06-07-2005, 10:45 AM
  2. write a word to file by using lseek and mmap
    By SoFarAway in forum C Programming
    Replies: 1
    Last Post: 03-28-2005, 12:33 PM
  3. problem with lseek system call on unix
    By kheinz in forum C Programming
    Replies: 2
    Last Post: 06-03-2003, 12:50 PM
  4. Positioning the cursor where I want it?
    By SPiRiToFCaT in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2002, 11:08 PM
  5. Cursor Positioning
    By rdnjr in forum Linux Programming
    Replies: 2
    Last Post: 12-09-2001, 11:57 PM

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