Ask a Question related to UNIX Programming, Design and Development.
-
Fletcher Glenn #1
Re: fseek backwards, overwrite partial, drop residue?
Jem Berkes wrote:
Easy. First fseek, then ftruncate, then fput, fwrite or whatever.>
> I'm doing something with a FILE* (opened in w+b) that seems like it should
> be easy enough:
>
> 1. Write lines to the file, occasionally using ftell to record a position
> 2. Occasionally use fseek to return back to earlier recorded position
> 3. After reposition, overwrite previous data - nothing beyond should exist
> 4. After all of that is done, rewind()
> 5. Again read lines at a time
>
> I don't want any of the previous file data to exist when I fseek and then
> write new line(s). Is there a way I can define some position as the EOF? Or
> otherwise drop everything past the current file stream position?
>
> I considered using ftell() after the 'overwrite' of line data to define for
> myself the new "file end point", but this doesn't work too well because in
> step 5 I need to process a line at a time - meaning that I would keep
> having to poll ftell() and see if it has exceeded my earlier marker. I'm
> looking for a better solution.
>
> Hope I've been able to explain my problem properly.
--
Fletcher Glenn
email [email]f-g-l-e-n-n@quest.com[/email] (remove the dashes)
Fletcher Glenn Guest
-
#40726 [NEW]: fseek / ftell do not work correctly for files > 2GB
From: plamen at pulsator dot com Operating system: FreeBSD 6.2 i386 PHP version: 5.2.1 PHP Bug Type: Filesystem function... -
#22682 [Fbk->NoF]: Proplems with fread(), ftell() and fseek() and CRLF newlines
ID: 22682 Updated by: sniper@php.net Reported By: fbeyer at clickhand dot de -Status: Feedback +Status: ... -
#22682 [Ana->Fbk]: Proplems with fread(), ftell() and fseek() and CRLF newlines
ID: 22682 Updated by: sniper@php.net Reported By: fbeyer at clickhand dot de -Status: Analyzed +Status: ... -
#22682 [Com]: Proplems with fread(), ftell() and fseek() and CRLF newlines
ID: 22682 Comment by: henrik dot gebauer at web dot de Reported By: fbeyer at clickhand dot de Status: Analyzed... -
How do I get XP to stop showing partial drop down menus
I know there is a setting in XP to show entire drop-down menus but I can't find it. Right now Outlook and Office programs show an abbreviated menu... -
Jem Berkes #2
Re: fseek backwards, overwrite partial, drop residue?
>> I don't want any of the previous file data to exist when I fseek and
>> then write new line(s). Is there a way I can define some position as
>> the EOF? Or otherwise drop everything past the current file stream
>> position?Thanks, I wasn't aware of ftruncate and fron the description it does appear> Easy. First fseek, then ftruncate, then fput, fwrite or whatever.
to do what I want. But I couldn't even get it to work in a simple demo
program, could it be that this does not work from a FILE* ? The output of
the following should be just "hello", but instead the full "hello world"
remains.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
char buf[512];
FILE* temp = tmpfile();
fputs("hello world", temp); /* 12 bytes written */
fseek(temp, 4, SEEK_SET); /* seek before cut point */
ftruncate(fileno(temp), 5); /* truncate file to 5 bytes */
rewind(temp);
while (fgets(buf, sizeof(buf), temp))
fputs(buf, stdout); /* show what's in the file */
return fclose(temp);
}
Jem Berkes Guest
-
Derk Gwen #3
Re: fseek backwards, overwrite partial, drop residue?
Jem Berkes <jb@users.pc9.org> wrote:
# >> I don't want any of the previous file data to exist when I fseek and
# >> then write new line(s). Is there a way I can define some position as
# >> the EOF? Or otherwise drop everything past the current file stream
# >> position?
#
# > Easy. First fseek, then ftruncate, then fput, fwrite or whatever.
#
# Thanks, I wasn't aware of ftruncate and fron the description it does appear
# to do what I want. But I couldn't even get it to work in a simple demo
# program, could it be that this does not work from a FILE* ? The output of
# the following should be just "hello", but instead the full "hello world"
# remains.
#
# #include <stdio.h>
# #include <unistd.h>
# #include <sys/types.h>
#
# int main()
# {
# char buf[512];
# FILE* temp = tmpfile();
# fputs("hello world", temp); /* 12 bytes written */
# fseek(temp, 4, SEEK_SET); /* seek before cut point */
# ftruncate(fileno(temp), 5); /* truncate file to 5 bytes */
# rewind(temp);
You have to be careful when mixing FILE* and file designator calls (stdio and kernel
calls). The FILE* functions can buffer output which the file designator functions
don't know about. You can use fflush(temp) to force bufferred output to the file.
--
Derk Gwen [url]http://derkgwen.250free.com/html/index.html[/url]
No pleasure, no rapture, no exquiste sin greater than central air.
Derk Gwen Guest
-
Jem Berkes #4
Re: fseek backwards, overwrite partial, drop residue?
> You have to be careful when mixing FILE* and file designator calls
Thanks, that's a good point. Adding fflush after ftruncate yields the> (stdio and kernel calls). The FILE* functions can buffer output which
> the file designator functions don't know about. You can use
> fflush(temp) to force bufferred output to the file.
correct results.
Is this technique (fseek, ftruncate, fflush) going to be portable enough?
I'm in the process of writing a mail filter that's going to run across most
*nix'es, and I have never used ftruncate before so I'm a bit nervous. All I
have tested this on so far is Linux libc 2.3 and SunOS. Also, any idea why
gcc -Wall -ansi gives warnings:
test.c:11: warning: implicit declaration of function `ftruncate'
test.c:11: warning: implicit declaration of function `fileno'
I don't see any reason for the warning, but again since I have not used
these functions before in projects I am a bit nervous.
Jem Berkes Guest
-
Fletcher Glenn #5
Re: fseek backwards, overwrite partial, drop residue?
"Jem Berkes" <jb@users.pc9.org> wrote in message
news:Xns93D02296C174jbuserspc9org@205.200.16.73...most>> > You have to be careful when mixing FILE* and file designator calls
> > (stdio and kernel calls). The FILE* functions can buffer output which
> > the file designator functions don't know about. You can use
> > fflush(temp) to force bufferred output to the file.
> Thanks, that's a good point. Adding fflush after ftruncate yields the
> correct results.
>
> Is this technique (fseek, ftruncate, fflush) going to be portable enough?
> I'm in the process of writing a mail filter that's going to run acrossI> *nix'es, and I have never used ftruncate before so I'm a bit nervous. Allftruncate in declared in /usr/include/unistd.h on my Solaris system.> have tested this on so far is Linux libc 2.3 and SunOS. Also, any idea why
> gcc -Wall -ansi gives warnings:
>
> test.c:11: warning: implicit declaration of function `ftruncate'
> test.c:11: warning: implicit declaration of function `fileno'
>
> I don't see any reason for the warning, but again since I have not used
> these functions before in projects I am a bit nervous.
Usually the stuff in unistd.h is widely found on most unix systems.
If your getting these warnings, you could try a "gcc -E" which should
allow you to see the preprocessed source before it is passed to
the compiler code.
--
Fletcher Glenn
to reply remove NOSPAM from my reply address
Fletcher Glenn Guest
-
Derk Gwen #6
Re: fseek backwards, overwrite partial, drop residue?
# > I don't see any reason for the warning, but again since I have not used
# > these functions before in projects I am a bit nervous.
#
# ftruncate in declared in /usr/include/unistd.h on my Solaris system.
# Usually the stuff in unistd.h is widely found on most unix systems.
# If your getting these warnings, you could try a "gcc -E" which should
# allow you to see the preprocessed source before it is passed to
# the compiler code.
Also
find /usr/include -name '*.h' -print | xargs grep -s ftruncate | more
can also drag recalcitrant definitions into the blinding white light
on most unices.
--
Derk Gwen [url]http://derkgwen.250free.com/html/index.html[/url]
Wow. A sailboat.
Derk Gwen Guest



Reply With Quote

