Thread: writing to a file after using lseek

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

    writing to a file after using lseek

    If I set lseek to say an offset of 20000 after the END of file, why can't I write the string "hello" at this position??

    Code:
    /*if you read data after end of file, buffer displays NULL*/
    
    #include <stdio.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    #define BUFFERSIZE 4096
    
    int main(int ac, char *argv[])
    {
    	int fd1;
    	ssize_t nread,nwrite,nbytes ;
    	off_t newpos;
    	char buf[BUFFERSIZE];
    	
    	fd1 = open("b.txt", O_RDWR);
    		
    	if (fd1 == -1)
    		perror("error");
    	
    	//offset 20000 positions after end of file
    	newpos = lseek(fd1, 20000, SEEK_END);
    
    	nread = (fd1,buf,20);
    	
    	printf("buffer contains: %s\n", buf);
    
    	strcpy(buf, "hello");
    	nbytes = strlen(buf);
    	printf("buffer contains: %s\n", buf);
    	
    	nwrite = (fd1,buf,20);
    
    	if (nwrite == -1)
    		perror("error here");
    
    
    	
    
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Putting everything else aside, what do you think lines #24 and #32 are doing?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    Quote Originally Posted by GReaper View Post
    Putting everything else aside, what do you think lines #24 and #32 are doing?
    thanks for reply. This is what I think L24 and L32 does

    line 24 --> take 20 haracters from file identified by fd and place them in buffer.

    I'm assuming this takes 20 charcters at the "20000th position"

    line 32 --> Write to file from buffer 20 characters at a time
    I'm assuming it will write hello and a few NULL characters

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Writing data to a position beyond the end of the file does not magically extend the file's size to that point where the write took place. You must first extend the file's size... a search of "teh interwebz" shows references to functions such as truncate/ftruncate/_chsize... etc. I don't know about them, haven't ever used them so I cannot offer any advice about their usage.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by bos1234 View Post
    thanks for reply. This is what I think L24 and L32 does

    line 24 --> take 20 haracters from file identified by fd and place them in buffer.

    I'm assuming this takes 20 charcters at the "20000th position"

    line 32 --> Write to file from buffer 20 characters at a time
    I'm assuming it will write hello and a few NULL characters
    You should take a closer look at those lines. To give you a hint: look up Comma operator - Wikipedia, the free encyclopedia
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by GReaper View Post
    You should take a closer look at those lines. To give you a hint: look up Comma operator - Wikipedia, the free encyclopedia
    Better hint: What (if there is one) function being called on those two lines?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lseek. Positioning cursor at end of file problem
    By bos1234 in forum C Programming
    Replies: 4
    Last Post: 04-01-2012, 12:09 PM
  2. What will happen if lseek() is wrong?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 04-10-2008, 11:17 PM
  3. lseek error
    By crash88 in forum Linux Programming
    Replies: 4
    Last Post: 07-18-2006, 09:01 AM
  4. write a word to file by using lseek and mmap
    By SoFarAway in forum C Programming
    Replies: 1
    Last Post: 03-28-2005, 01:33 PM
  5. What does lseek do?
    By simly01 in forum C++ Programming
    Replies: 0
    Last Post: 07-29-2002, 03:45 PM