lseek( ) on file opened with O_APPEND

I noticed that when I opened a file using open( ) with the O_APPEND
flag that I have to
first write to the file in order to have lseek( ) return the offset
value equivalent to the offset
from the beginning of the file.  For example, the code below

    fd = open( my_file, O_APPEND | O_WRONLY );
    printf( "Current offset following lseek( fd, O, SEEK_CUR): %d\n",
        ( int ) lseek( fd, 0, SEEK_CUR ) );

will print the text

Current offset following lseek( fd, O, SEEK_CUR): 0

If I write to the file and then call lseek( fd, 0, SEEK_CUR ), I will
get the actual offset from
the beginning of the file

    fd = open( my_file, O_APPEND | O_WRONLY );
    printf( "Current offset following lseek( fd, O, SEEK_CUR): %d\n",
        ( int ) lseek( fd, 0, SEEK_CUR ) );
    bytes_written = write( fd, buffer, num_bytes );
    init_off = lseek( fd, 0, SEEK_CUR ) - (off_t ) bytes_written;
    printf( "Actual initial offset to end of file: %d\n", init_off );
    printf( "Current file offset: %d\n", ( int ) lseek( fd, 0,
SEEK_CUR ) );

will give me output 
 
Current offset following lseek( fd, 0, SEEK_CUR): 0
Actual initial offset to end of file: 12
Current file offset: 23  

Note that this suggests that the initial file size was 12 bytes, and
11 bytes were written to
fd.

In summary, lseek( fd, 0, SEEK_CUR ) will return (off_t ) 0 after a
file is opened for
appending.  Once at least one byte is written to fd, lseek( fd, 0,
SEEK_CUR ) will
return the offset from the beginning of the file.  Is there a better
way to get the offset to
the end of the file?  I could have opened the file without the
O_APPEND flag, and called
lseek( fd, 0, SEEK_END ), but this not safe if multiple processes are
appending to the same
file.  By the way, forgive my lack of error checking above; I have
omitted it for the sake of
clarity in this post, not in the code itself.

Thanks for your help,
Victor
0
weinstev
11/18/2003 7:55:37 PM
comp.unix.programmer 10729 articles. 0 followers. kokososo56 (349) is leader. Post Follow

1 Replies
1163 Views

Similar Articles

[PageSpeed] 33
In article <4c9cde18.0311181155.3a092f44@posting.google.com>,
Victor <weinstev@yahoo.com> wrote:
>In summary, lseek( fd, 0, SEEK_CUR ) will return (off_t ) 0 after a
>file is opened for
>appending.  Once at least one byte is written to fd, lseek( fd, 0,
>SEEK_CUR ) will
>return the offset from the beginning of the file.  

O_APPEND causes you to go to the end immediately before writing anything.
Until you try to write something, it doesn't have to reposition.

>						    Is there a better
>way to get the offset to
>the end of the file?  

Call fstat().

>		       I could have opened the file without the
>O_APPEND flag, and called
>lseek( fd, 0, SEEK_END ), but this not safe if multiple processes are
>appending to the same
>file.

If other processes are appending to the file, any method you use is
useless, since the file length can change after you call it.

-- 
Barry Margolin, barry.margolin@level3.com
Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
0
Barry
11/18/2003 8:53:40 PM
Reply:
Similar Artilces:

lseek
I am using fedora 17 and have found the macros SEEK_DATA and SEEK_HOLE do not work. So I used the ints 3 and 4 respectively for whence. Is this a common bug? 3 and 4 seem to work. Bill On 2012-08-15, Bill Cunningham <nospam@nspam.invalid> wrote: > I am using fedora 17 and have found the macros SEEK_DATA and SEEK_HOLE > do not work. So I used the ints 3 and 4 respectively for whence. Is this a > common bug? 3 and 4 seem to work. > > Bill > > Hi, I think you have to compile with -D_GNU_SOURCE . It is probably a documentation bug. p...

running executable file in target machine
I have labview 7.1 professional version. I have created a executable file for target machine. When the executable file run at target machine, it needs all drivers and run time engine. I have installed drivers in target machine but not run time engine. Do I have to create a installer file which includes run time engine? If I only have *.exe file instead of installer file, how can I install run time engine? Do I have to put all subvi in the same directory as the one when the program was developed? Thanks. I believe that you can install the runtime engine with your original professional versi...

Re: Ftp Error WARNING: End of file.Segmentation Violation In Task
I think you may find the explanation and resolution at: http://support.sas.com/kb/10/252.html HTH, Art -------- On Wed, 4 Feb 2009 13:09:24 -0800, sasguy <addanki007@GMAIL.COM> wrote: >WARNING: End of file. >Segmentation Violation In Task [ DATASTEP ] >Fault Occurred at [/opt/sas/sas91/hotfix/sasexe/sasxkern.e9bc16:yxrinfo >+0xf0] > >Task Traceback > >/opt/sas/sas91/hotfix/sasexe/sasxkern.e9bc16:yxrinfo+0xf0 >/opt/sas/sas91/sasexe/sasdsx:dseopen+0x4c >/opt/sas/sas91/sasexe/sasdsx:dsxxini+0x414 >/opt/sas/sas91/sasexe/sasds:dsexec+0x44 >/opt/sas/sas91/...

Oddity with Find::File and -M
I am having problems with Find::File and -M _ in that I get different values for directories depending on whether I process files or not. use strict; use warnings; use File::Find; my $d = shift || ''; #print time(), "\n"; $^T=1293201891; # fix basetime to keep numbers the same between runs find(\&notwanted, '.'); exit; sub notwanted { return if m/^\.$/; # Don't need '.' return if $d && ! -d; # only directories print $File::Find::dir,' ',$File::Find::name,' ',$_, ' ', -M _, " "; my ($de...

Unix help required: one line comand to date Column to a fixed length file parsing the file name
Hi , I have a fixed length file of records size 50 , file_nm.YYYYMMDD.dat . My requirement is that : To parse the file name for date and add the apped this date to the end of each record . After this the resultant record size becomes 58 . Is there one line command with AWK /SED i can make use for this purpose . Thanks in Advance , Ajay ajay.balki wrote: > Hi , > > I have a fixed length file of records size 50 , > file_nm.YYYYMMDD.dat . > My requirement is that : To parse the file name for date and add the > apped this date to the end of each record . > > Aft...

[9fans] ext2srv understands only 7bit ASCII file names?
I have some files on an external ext2 drive that have whitespace and umlauts (=C3=A4, =C3=B6) in them. trfs took care of the whitespace. But ext= 2srv presents umlauts as a question mark symbol (=EF=BF=BD) and won't let me acc= ess the file (error: file does not exist). Where is the problem? These files show correctly in linux. As a workaround I can certainly boot that other OS and rename the files. It's just every time I see that penguin I get a rash. On Thursday 13 of October 2011 13:15:57 slash wrote: > I have some files on an external ext2 drive that have whitespace and >...

Extracting data from excel file through java
I want to extract data from excel file through Java,based on some condition. I have searched on google and got POI but, cant I do it just using core java? I tried reading the excel file and got symbols... wht should I do? On 2008-06-01 12:52 +0100, ruds allegedly wrote: > I want to extract data from excel file through Java,based on some > condition. I have searched on google and got POI but, cant I do it > just using core java? Of course you can. Just like any other specific library does. The question is: do you really want go through the hassle? > I tried reading the excel fi...

Problem with lseek and read
Hi, I am working on Solaris 7. I am creating a 32-bit binary on a 64-bit platform. When I execute the application it fails in read. No value is set in errno and read returns a 0. I am using the read system call to read from the accounting file. Prior to invoking read I position the file pointer at a specific position using lseek. On debugging I found that replacing lseek with lseek64 or setting _FILE_OFFSET_BITS=64 resolved my problem. I am however not able to understand the reason behind the problem. The offset used with lseek is a multiple of 40. I initially thought that the problem was be...

lseek() returns legal negative value
Hello everybody. I have a question regarding lseek(). SUSv3 does say: "A negative file offset may be valid for some devices in some implementations." I know about one example: On SVR4, using lseek() against /proc/pid/mem on SVR4 might return a negative return value as legal value when addresses have the MSB set. Does someone know about other devices that would have such a unsual property? BTW, It shows than checking error condition for tradition syscall like: if (syscall() < 0) { /* error */ } should not be used... TIA, Loic loic-dev@gmx.net (Loic Domaigne) write...

how to check if script in a file a method?
hello, given a text string with ruby code, that my program evaluates at some point inside an anonymous module, is there a way to analyse the code without evaluating it inside a module to tell if it implements a particular method? i have a bunch of strings like that and i want to present the list of them to the user but i need to show what these scripts do so that she could choose. after that i do evaluate the chosen one in a module and use it. evaluating all of them and using respond_to? before displaying the selection list seems ugly. is parsing the only alternative? something simila...

Scripting for file transfer
I have the following in a csh script. This is run on a remote system and the goal is to allow a user on a client to tar and gzip files and download them to their system. All I get is streamed data to the browser. Any help would be appreciated. # ! /bin/csh echo "Content-description: File Transfer'\n" echo "'Content-type: application/x-gzip\n'" echo "Content-disposition: attachment; filename=\"site.tar.gz\"\n"; # < do some file redirection in here -> tar cf - * | gzip -c #end The interesting thing is if I do: tar cf - * | gzip ...

Re: How to check if a file exists on webdav?
hmm, did you try it with fileexist without a filename? Like: x=fileexist("the_file"); Gerhard On Wed, 2 Dec 2009 09:00:33 -0800, Yat John Pang <y.j.pang@GMAIL.COM> wrote: >Hi all, I have the following sas challenge (9.1.3). Hopefully someone >dealt with this problem before. I want to check if an excel file >exists on webdav before I get the file over to the workspace. > >I have tried the filename (sasxbamw) then use fexist. But somehow the >return code is always 1 (even it does not exists). > >Does anyone know how to ch...

COBOL file status 9P after DDS change
We had a strange problem this morning resulting from a change that went in last night. We have a CL program that performs a STRCMTCTL, then calls COBOL program 1 which puts file A under Commit Control, then COBOL program 1 calls COBOL program 2 which also uses file A but not under Commit Control, when COBOL program 2 finishes it returns control to COBOL program 1, when COBOL program 1 finishes it returns control to the CL program, which performs an ENDCMTCTL. This all works fine. Last night, we amended the layout of file A by adding some extra fields to the end. We also added a new logical...

writing into a text file
Hi, I have a small issue: I have a text file which contains text and numbers. I'm interested in replacing the numbers matrix with another matrix of the same size. For example: this is the file that I have: hello world how are you data 0 1 2 3 4 5 6 8 11 22 33 55 end of data EOF I'm interested in replacing the 3x4 matrix of the numbers, with another. How would you suggest me to do that? thanks Evgeny Lahav wrote: > I have a text file which contains text and numbers. > I'm interested in replacing the numbers matrix with another matrix of the same size. > For example: ...

Standard MIDI Files
Hello Mayby my questions are silly (I hope they aren't) but I'm not a musican and I'm MIDI beginer. Is all voice messages in single track (midifile format 1) *have to* use only one channel? Is first track *have to* contains all time signature, key signature and tempo events? Is key signature meta-event affect notes in midifile or has only informational character? I.e. when I have 5 sharps and A (69) occur is this A (69) or is this A# (70)? Pitch bend applies only to notes which are curently playing or to all notes that will be played soon too? I.e. when A and F are playin...

RS/GIS job opening
Organization: OSU Department of Geography Title: Lab Coordinator Location: Stillwater, OK Application Deadline: Posted: 2008-07-20 Position Description: Coordinator for the Center for the Application of Remote Sensing (CARS). Oversee the coordination in all information technologies (IT) for the Department of Geography. Manage entire department's computers, departmental servers, and a wide array of Global Positioning System units that support instruction and research activities. Masters degree in Geography or closely related discipline. Extensive experience in remote sensing, Ge...

Using Linux, how can I combine an audio file and a still picture to make video YouTube will accept?
I'm starting off with a *.jpg and an MP3 file. I would like to combine the audio, with the still picture, then upload it to YouTube. Does Linux have any easy apps with clear directions for doing this? Thanks Steve On 13/10/14 03:44, Steve wrote: > > I'm starting off with a *.jpg and an MP3 file. > > I would like to combine the audio, with the still picture, then upload it to YouTube. > > Does Linux have any easy apps with clear directions for doing this? > > Thanks > Steve > Can't you do that with Youtube's online editor?...

problems opening DBF and NTX files on Win XP
I have an old application for DOS, but now I want to use it on a WinXP (Pro), but when the app try to open a file it put me the errors: Error DBFNTX/1003 Open Error: TEST71.ntx (DOS ERROR 4) and Error DBFNTX/1001 Open Error: TEST6.DBF (DOS ERROR 4) I knowed that I have to edit the file config.nt adding the line FILES = xxx (I put 1000!!), I also tried to change the value for TEMP and TMP variables without any space... but it still doesn't work.. any other suggestion??? thank'you Emanuele P.S. I am not a clipper developer -- Posted via http://dbforums.com Ross & Emanuele ...

no startup-file
Is there a command line option available, so that the file startup.m is not evaluated? I think I had seen such a option in former versions, but cannot find it for R2006a. Greets Michael Michael wrote: > Is there a command line option available, so that the file startup.m is > not evaluated? I think I had seen such a option in former versions, but > cannot find it for R2006a. > > Greets > Michael Just be sure to start somewhere where startup.m won't be on your path. -- --Loren http://blogs.mathworks.com/loren/ "Michael" <MKawohl@gmx.de> wrote in...

Mopier printing of merged PS files
Each year my wife publishes a booklet for a local festival. It is consists of a cover, the main body, and an entry form. The cover is printed in booklet format on colored letter size paper, the body is printed in booklet format on plain letter size paper and the entry form is a normal letter size and format, printed on letter size paper. In the past she prints all three as separate print jobs and hand collates, folds and staples them, all 2,000+ copies. I would like to 'merge' all three print jobs and have the printer (HP 4050 LaserJet) do the collating for her. The cover would print ...

Lifting File
Hi, I have basic knowledge in unix in administration on SOLARIS and LINUX. I was asked this following question when I said I had basic knowledge in UNIX. How do you LIFT A FILE in UNIX? To be honest, I never heard of such concept. Could someone help me with this please regards, sarat On 2008-10-15, sarat <sarat911@gmail.com> wrote: > Hi, > I have basic knowledge in unix in administration on SOLARIS and > LINUX. I was asked this following question when I said I had basic > knowledge in UNIX. > > How do you LIFT A FILE in UNIX? > > To be h...

creating self executable files from m.file
i have a problem about compiling m.file . i want to run my .exe file in a computer which matlab is not installed. But the .exe file looks for some matlab files and also an error occurs called java virtual machine could not be started . i compiled my m file with this command : mcc -m myfile.m regards, On Mon, 18 Jul 2005 10:42:47 -0400, ismail meri´┐Ż can uygan wrote: > i have a problem about compiling m.file . i want to run my .exe file > in a computer which matlab is not installed. But the .exe file looks > for some matlab files and also an error occurs called java virtual > mac...

A simple file system filter driver
Hi all, I am new at developing file system filter driver. I need intercepting all file operations in a file system filter driver dispatch routine and block certain operations using the code below: Irp->IoStatus.Status = STATUS_ACCESS_DENIED; IoCompleteRequest(Irp, IO_NO_INCREMENT); return STATUS_ACCESS_DENIED; 1. I have examined filemon and sfilter source code, but they are really very detailed for my project. Can I get any idea about a simple one or an orientation about the subject? 2. How can I determine about system related work in my driver dispatch routine? Filemo...

I've filed away the song
Unfortunately for you, Snit the lying troll, I've "filed" the words of my song away -- and this being a Linux system, you just *know* that that I'll never be able to find those lyrics again. Oh, why oh why don't I have a Mac, where the "typewriter and paper are one?" Oh, wait a minute, here they are: "Come listen to the story of Snit the lying troll Couldn't speak a word, if lying weren't his goal Then on COLA, while spewing out some FUD Out from his gob oozed some bubblin' crud. Bullshit, that is Micro$haft tea..." -- RonB "There...

How translate sdt iii files to orcad 9.2
Hello, I have a lot off old lib and sch files in orcad 3.22 but don't know how to convert them into my new computer running in windows 98 with orcad 9.2 who can help me. best regards martin \ ...