lseek function
5 posters
lseek function
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 ?
{
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
Re: lseek function
hey guess since u hv given O_APPEND....it is appending to end of file...
try without tat
try without tat
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Re: lseek function
but if u use 0_RDWR it will overwrite the previous contents
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Re: lseek function
- 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.
Re: lseek function
v=lseek(fd,-3,2);
What does this do?
What does this do?
jennyinhere- Posts : 24
Points : 30
Join date : 2010-02-26
Age : 35
Re: lseek function
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?
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
Re: lseek function
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
Re: lseek function
[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'??
[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
Re: lseek function
The o/p tat she wants is abcdefghijklmnopqrstmithuuvwxyz
jennyinhere- Posts : 24
Points : 30
Join date : 2010-02-26
Age : 35
Re: lseek function
@ 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
Re: lseek function
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.
Re: lseek function
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
Re: lseek function
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);
}
#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
Re: lseek function
Here is sample Pgm(to insert in the middle).
File before Insertion
C pgm
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);
}
- Code:
abcdefghijklmnopqrstVERY BORING DAYuvwxyz
Permissions in this forum:
You cannot reply to topics in this forum
|
|