lseek function

Go down

lseek function

Post by Maithreyi on Fri Mar 12, 2010 6:38 pm

main()
{
char buf[10]="mithu";
int fd,v;
fd=open("obc.txt",O_RDWR|O_APPEND,0000700);
v=lseek(fd,-3,2);
if(v==-1)
{
printf("seek error");
}
else
{
write(fd,buf,strlen(buf));
}
}
File obc.txt before program execution
[335818@oracleclient ~]$ cat obc.txt
abcdefghijklmnopqrstuvwxyz
Now,
[335818@oracleclient ~]$ gcc fileseek.c
[335818@oracleclient ~]$ ./a.out
[335818@oracleclient ~]$ cat obc.txt
abcdefghijklmnopqrstmithuz.
Suppose, I add O_APPEND in the mode,the output becomes abcdefghijklmnopqrstuvwxyzmithu
I want the output as abcdefghijklmnopqrstmithuuvwxyz without overwriting.Any clue ?

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: lseek function

Post by akalya on Fri Mar 12, 2010 7:19 pm

hey guess since u hv given O_APPEND....it is appending to end of file...
try without tat

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

View user profile

Back to top Go down

Re: lseek function

Post by akalya on Fri Mar 12, 2010 7:20 pm

but if u use 0_RDWR it will overwrite the previous contents Sad

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

View user profile

Back to top Go down

Re: lseek function

Post by Christopher on Sat Mar 13, 2010 1:09 am

Code:
#include<stdio.h>
#include<fcntl.h>

int main()
{
   int fd;
   fd=open("abc.txt",O_RDWR,0755);
   printf("File opened : %d\n",fd);
   lseek(fd,15,SEEK_SET);
   write(fd,"abcdefghijklmnopqrstuvwxyz",26);
   close(fd);
}

in above code, it ll insert from position 15 but it overwrites the next 26 character after that to store 'a-z'. if u don want to overwrite, copy the contents to another buffer and append it at the end.
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: lseek function

Post by jennyinhere on Mon Mar 15, 2010 1:10 pm

v=lseek(fd,-3,2);

What does this do?

jennyinhere

Posts : 24
Points : 30
Join date : 2010-02-26
Age : 30

View user profile

Back to top Go down

Re: lseek function

Post by Christopher on Mon Mar 15, 2010 1:13 pm

-3 is offset and 2 is equivalent to SEEK_END. It means 3rd character from last.
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: lseek function

Post by jennyinhere on Mon Mar 15, 2010 1:19 pm

ok
In the program posted by maithreyi , does lseek mean that the buf contents should get inserted in between the file contents , the position being 3rd frm end?

jennyinhere

Posts : 24
Points : 30
Join date : 2010-02-26
Age : 30

View user profile

Back to top Go down

Re: lseek function

Post by Christopher on Mon Mar 15, 2010 1:24 pm

yes Smile
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: lseek function

Post by jennyinhere on Mon Mar 15, 2010 1:31 pm

In the output above , why is the insertion takin place after 't' ? It shd tak place after 'w' right??

jennyinhere

Posts : 24
Points : 30
Join date : 2010-02-26
Age : 30

View user profile

Back to top Go down

Re: lseek function

Post by Christopher on Mon Mar 15, 2010 1:34 pm

it was not the output, she wants output like tat.
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: lseek function

Post by jennyinhere on Mon Mar 15, 2010 1:37 pm

[335818@oracleclient ~]$ gcc fileseek.c
[335818@oracleclient ~]$ ./a.out
[335818@oracleclient ~]$ cat obc.txt
abcdefghijklmnopqrstmithuz.


She has shown this as the o/p.

I want to know why insertion is takin place after 't'??

jennyinhere

Posts : 24
Points : 30
Join date : 2010-02-26
Age : 30

View user profile

Back to top Go down

Re: lseek function

Post by jennyinhere on Mon Mar 15, 2010 1:39 pm

The o/p tat she wants is abcdefghijklmnopqrstmithuuvwxyz

jennyinhere

Posts : 24
Points : 30
Join date : 2010-02-26
Age : 30

View user profile

Back to top Go down

Re: lseek function

Post by Maithreyi on Mon Mar 15, 2010 1:57 pm

@ Christopher, The point is I don't want to append it to the end.I want it inserted at the particular position I seek.Change of file creation mode might help.But I have no clue what that mode is!

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: lseek function

Post by Christopher on Mon Mar 15, 2010 2:10 pm

i think inserting to a sequential file is not possible. If u want to insert in the middle, seek to tat particular location, read from tat position to the end of file and store it in a buffer. then start writing from the position and append the buffer at the end.
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: lseek function

Post by Maithreyi on Mon Mar 15, 2010 2:12 pm

Ya thats there! In case you get a clue on this, do post in!

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: lseek function

Post by Nazneennazim on Mon Mar 15, 2010 2:16 pm

I want to append the remaining text in abc.txt after a-z is printed.This is the code that I tried.It does not show error but does not print the remaining text.

#include <stdio.h>
#include <fcntl.h>

int main()
{
int fd;
char* buffer;
fd=open("abc.txt",O_RDWR,0755);
printf("File opened : %d\n",fd);
lseek(fd,15,SEEK_SET);
read(fd,buffer,26);
write(fd,"abcdefghijklmnopqrstuvwxyz",26);
write(fd,buffer,26);
close(fd);
}

Nazneennazim

Posts : 10
Points : 16
Join date : 2010-03-01

View user profile

Back to top Go down

Re: lseek function

Post by Christopher on Mon Mar 15, 2010 2:24 pm

Here is sample Pgm(to insert in the middle).

File before Insertion
Code:
abcdefghijklmnopqrstuvwxyz

C pgm
Code:
#include<stdio.h>
#include<fcntl.h>

#define STARTPOS 20
#define MAXSIZE 1000

int main()
{
  int fd=open("1.txt",O_RDWR,0000700);
  int v=lseek(fd,STARTPOS,SEEK_SET);
  char buf[MAXSIZE]="";
  read(fd,buf,MAXSIZE);
  lseek(fd,STARTPOS,SEEK_SET);
  write(fd,"VERY BORING DAY",15);
  write(fd,buf,MAXSIZE);
}
File after insertion
Code:
abcdefghijklmnopqrstVERY BORING DAYuvwxyz
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: lseek function

Post by Christopher on Mon Mar 15, 2010 4:02 pm

@nazeen nazim
can u elaborate ur question?
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: lseek function

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum