Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

lseek function

5 posters

Go down

lseek function Empty lseek function

Post by Maithreyi 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 : 36
Location : Haldia

Back to top Go down

lseek function Empty Re: lseek function

Post by akalya 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

Back to top Go down

lseek function Empty Re: lseek function

Post by akalya 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

Back to top Go down

lseek function Empty Re: lseek function

Post by Christopher 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.
Christopher
Christopher
Admin

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

https://unixcpp.forumotion.com

Back to top Go down

lseek function Empty Re: lseek function

Post by jennyinhere 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 : 35

Back to top Go down

lseek function Empty Re: lseek function

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

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

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

https://unixcpp.forumotion.com

Back to top Go down

lseek function Empty Re: lseek function

Post by jennyinhere 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 : 35

Back to top Go down

lseek function Empty Re: lseek function

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

yes Smile
Christopher
Christopher
Admin

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

https://unixcpp.forumotion.com

Back to top Go down

lseek function Empty Re: lseek function

Post by jennyinhere 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 : 35

Back to top Go down

lseek function Empty Re: lseek function

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

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

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

https://unixcpp.forumotion.com

Back to top Go down

lseek function Empty Re: lseek function

Post by jennyinhere 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 : 35

Back to top Go down

lseek function Empty Re: lseek function

Post by jennyinhere 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 : 35

Back to top Go down

lseek function Empty Re: lseek function

Post by Maithreyi 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 : 36
Location : Haldia

Back to top Go down

lseek function Empty Re: lseek function

Post by Christopher 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.
Christopher
Christopher
Admin

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

https://unixcpp.forumotion.com

Back to top Go down

lseek function Empty Re: lseek function

Post by Maithreyi 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 : 36
Location : Haldia

Back to top Go down

lseek function Empty Re: lseek function

Post by Nazneennazim 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

Back to top Go down

lseek function Empty Re: lseek function

Post by Christopher 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
Christopher
Christopher
Admin

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

https://unixcpp.forumotion.com

Back to top Go down

lseek function Empty Re: lseek function

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

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

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

https://unixcpp.forumotion.com

Back to top Go down

lseek function Empty 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