fseek backwards, overwrite partial, drop residue?

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default Re: fseek backwards, overwrite partial, drop residue?

    Jem Berkes wrote:
    >
    > 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.
    Easy. First fseek, then ftruncate, then fput, fwrite or whatever.

    --
    Fletcher Glenn
    email [email]f-g-l-e-n-n@quest.com[/email] (remove the dashes)
    Fletcher Glenn Guest

  2. Similar Questions and Discussions

    1. #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...
    2. #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: ...
    3. #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: ...
    4. #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...
    5. 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...
  3. #2

    Default 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?
    > 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);
    while (fgets(buf, sizeof(buf), temp))
    fputs(buf, stdout); /* show what's in the file */
    return fclose(temp);
    }

    Jem Berkes Guest

  4. #3

    Default 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

  5. #4

    Default Re: fseek backwards, overwrite partial, drop residue?

    > 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 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

  6. #5

    Default Re: fseek backwards, overwrite partial, drop residue?


    "Jem Berkes" <jb@users.pc9.org> wrote in message
    news:Xns93D02296C174jbuserspc9org@205.200.16.73...
    > > 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 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.
    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.

    --
    Fletcher Glenn
    to reply remove NOSPAM from my reply address


    Fletcher Glenn Guest

  7. #6

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139