How to fputc 'EOF' to a FILE stream.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • oksuresh@gmail.com

    How to fputc 'EOF' to a FILE stream.

    Hi talents,

    I have a situation where , I should keep on reading a FILE stream until
    a location. And I have to immediately write the EOF character , so that
    the rest of the file is cleared.

    Any idea?....

    Thanks in Advance
    Suresh

  • Ben Pfaff

    #2
    Re: How to fputc 'EOF' to a FILE stream.

    oksuresh@gmail. com writes:
    [color=blue]
    > I have a situation where , I should keep on reading a FILE stream until
    > a location. And I have to immediately write the EOF character , so that
    > the rest of the file is cleared.[/color]

    This is a FAQ.

    19.13: How can a file be shortened in-place without completely clearing
    or rewriting it?

    A: BSD systems provide ftruncate(), several others supply chsize(),
    and a few may provide a (possibly undocumented) fcntl option
    F_FREESP. Under MS-DOS, you can sometimes use write(fd, "", 0).
    However, there is no portable solution, nor a way to delete
    blocks at the beginning. See also question 19.14.
    --
    int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
    );}return 0;}

    Comment

    • Keith Thompson

      #3
      Re: How to fputc 'EOF' to a FILE stream.

      oksuresh@gmail. com writes:[color=blue]
      > I have a situation where , I should keep on reading a FILE stream until
      > a location. And I have to immediately write the EOF character , so that
      > the rest of the file is cleared.[/color]

      EOF is not a character. EOF is a macro that expands to a negative
      constant that's used to indicate an end-of-file condition. (That
      condition might be indicated by some character on some systems, but
      that's system-specific, and is not related to EOF.)

      See Ben Pfaff's answer.

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
      We must do something. This is something. Therefore, we must do this.

      Comment

      • CBFalconer

        #4
        Re: How to fputc 'EOF' to a FILE stream.

        oksuresh@gmail. com wrote:[color=blue]
        >
        > I have a situation where , I should keep on reading a FILE stream
        > until a location. And I have to immediately write the EOF character,
        > so that the rest of the file is cleared.[/color]

        There is no EOF character. There is only an EOF condition of a
        stream. The only portable way to handle your requirement is to
        copy the file until you reach the stopping point. Then close both
        the file and the new copy.

        EOF is a negative int value, not a char value. It is distinct from
        all characters. You don't know what it is, but you do know it is
        defined in stdio.h. Streams return that value to signal that they
        have encountered the end-of-file condition, i.e. there is no more
        data.

        --
        "If you want to post a followup via groups.google.c om, don't use
        the broken "Reply" link at the bottom of the article. Click on
        "show options" at the top of the article, then click on the
        "Reply" at the bottom of the article headers." - Keith Thompson
        More details at: <http://cfaj.freeshell. org/google/>
        Also see <http://www.safalra.com/special/googlegroupsrep ly/>


        Comment

        • Richard Bos

          #5
          Re: How to fputc 'EOF' to a FILE stream.

          CBFalconer <cbfalconer@yah oo.com> wrote:
          [color=blue]
          > oksuresh@gmail. com wrote:[color=green]
          > >
          > > I have a situation where , I should keep on reading a FILE stream
          > > until a location. And I have to immediately write the EOF character,
          > > so that the rest of the file is cleared.[/color]
          >
          > There is no EOF character.[/color]

          This is not entirely true, which is probably much of the reason for this
          confusion.
          There _is_ an EOF character in ASCII (and possibly also in other
          charsets), but it is only indirectly related to the C EOF macro, and
          most definitely does not have the same value[1]. The two should not be
          confused, but it's easy for a newbie to do so.

          Richard

          [1] Because all ASCII characters are zero-or-positive, while EOF < 0.

          Comment

          • CBFalconer

            #6
            Re: How to fputc 'EOF' to a FILE stream.

            Richard Bos wrote:[color=blue]
            > CBFalconer <cbfalconer@yah oo.com> wrote:[color=green]
            >> oksuresh@gmail. com wrote:[color=darkred]
            >>>
            >>> I have a situation where , I should keep on reading a FILE stream
            >>> until a location. And I have to immediately write the EOF character,
            >>> so that the rest of the file is cleared.[/color]
            >>
            >> There is no EOF character.[/color]
            >
            > This is not entirely true, which is probably much of the reason for
            > this confusion. There _is_ an EOF character in ASCII (and possibly
            > also in other charsets), but it is only indirectly related to the C
            > EOF macro, and most definitely does not have the same value[1]. The
            > two should not be confused, but it's easy for a newbie to do so.[/color]

            No, there is NO EOF character, especially in ASCII. Some systems
            assign some character, or character sequence, to signal EOF. Some
            historically popular choices have been:

            ^Z SUB Who knows why, maybe because the last letter
            ^D EOT End of Transmission
            ^Y EM End of medium
            ^[ ESC Escape
            ^\ FS File Separator
            ^] GS Group Separator
            ^^ RS Record Separator
            ^_ US Unit Separator

            The most commonly used sequences are: CR SUB and CR EOT as seen at
            a keyboard. One system used ":EOF" at the immediate head of a line
            only, i.e. preceded by CR.

            Another generic means of signalling EOF from the keyboard is the
            power switch. This one may have undesirable side effects.

            --
            "The power of the Executive to cast a man into prison without
            formulating any charge known to the law, and particularly to
            deny him the judgement of his peers, is in the highest degree
            odious and is the foundation of all totalitarian government
            whether Nazi or Communist." -- W. Churchill, Nov 21, 1943


            Comment

            • Keith Thompson

              #7
              Re: How to fputc 'EOF' to a FILE stream.

              CBFalconer <cbfalconer@yah oo.com> writes:[color=blue]
              > oksuresh@gmail. com wrote:[color=green]
              >>
              >> I have a situation where , I should keep on reading a FILE stream
              >> until a location. And I have to immediately write the EOF character,
              >> so that the rest of the file is cleared.[/color]
              >
              > There is no EOF character. There is only an EOF condition of a
              > stream. The only portable way to handle your requirement is to
              > copy the file until you reach the stopping point. Then close both
              > the file and the new copy.
              >
              > EOF is a negative int value, not a char value. It is distinct from
              > all characters. You don't know what it is, but you do know it is
              > defined in stdio.h. Streams return that value to signal that they
              > have encountered the end-of-file condition, i.e. there is no more
              > data.[/color]

              One more potential stumbling block: if char is signed, and EOF is,
              say, -1, then EOF (or rather the value of what the EOF macro expands
              to) *can* be a valid value of type char.

              fgetc() and friends return either the value EOF, or a character value
              *as an unsigned char converted to int*. A program that incorrectly
              stores there result of fgetc() in a char object can mistakenly
              interpret valid character data as EOF.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              • Barry Schwarz

                #8
                Re: How to fputc 'EOF' to a FILE stream.

                On 14 Feb 2006 21:40:46 -0800, oksuresh@gmail. com wrote:
                [color=blue]
                >Hi talents,
                >
                >I have a situation where , I should keep on reading a FILE stream until
                >a location. And I have to immediately write the EOF character , so that
                >the rest of the file is cleared.
                >
                >Any idea?....
                >[/color]
                C does not define an EOF character. Some systems define an EOF
                character for certain uses (e.g., Windows uses 0x1a as an EOF
                character in a text file, but not in a binary one). If your system
                supports an EOF character, you would find out about it a newsgroup
                that deals specifically with your system.


                Remove del for email

                Comment

                • Keith Thompson

                  #9
                  Re: How to fputc 'EOF' to a FILE stream.

                  Barry Schwarz <schwarzb@doezl .net> writes:[color=blue]
                  > On 14 Feb 2006 21:40:46 -0800, oksuresh@gmail. com wrote:
                  >[color=green]
                  >>Hi talents,
                  >>
                  >>I have a situation where , I should keep on reading a FILE stream until
                  >>a location. And I have to immediately write the EOF character , so that
                  >>the rest of the file is cleared.
                  >>
                  >>Any idea?....
                  >>[/color]
                  > C does not define an EOF character. Some systems define an EOF
                  > character for certain uses (e.g., Windows uses 0x1a as an EOF
                  > character in a text file, but not in a binary one). If your system
                  > supports an EOF character, you would find out about it a newsgroup
                  > that deals specifically with your system.[/color]

                  And even on such systems, writing an EOF character to a file may or
                  may not do anything useful.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                  We must do something. This is something. Therefore, we must do this.

                  Comment

                  • Richard Bos

                    #10
                    Re: How to fputc 'EOF' to a FILE stream.

                    CBFalconer <cbfalconer@yah oo.com> wrote:
                    [color=blue]
                    > Richard Bos wrote:[color=green]
                    > > CBFalconer <cbfalconer@yah oo.com> wrote:[color=darkred]
                    > >> oksuresh@gmail. com wrote:
                    > >>>
                    > >>> I have a situation where , I should keep on reading a FILE stream
                    > >>> until a location. And I have to immediately write the EOF character,
                    > >>> so that the rest of the file is cleared.
                    > >>
                    > >> There is no EOF character.[/color]
                    > >
                    > > This is not entirely true, which is probably much of the reason for
                    > > this confusion. There _is_ an EOF character in ASCII (and possibly
                    > > also in other charsets), but it is only indirectly related to the C
                    > > EOF macro, and most definitely does not have the same value[1]. The
                    > > two should not be confused, but it's easy for a newbie to do so.[/color]
                    >
                    > No, there is NO EOF character, especially in ASCII.[/color]

                    *Goes to look this up*

                    By the noodly appendages of Cthulhu-in-a-dress, you're right! I never
                    realised this...

                    Mind you, it doesn't contradict my point, really; all it does is move me
                    more firmly into the newbie camp :-/

                    Richard

                    Comment

                    • Dave Thompson

                      #11
                      Re: How to fputc 'EOF' to a FILE stream.

                      On Wed, 15 Feb 2006 14:08:52 -0500, CBFalconer <cbfalconer@yah oo.com>
                      wrote:
                      [color=blue]
                      > No, there is NO EOF character, especially in ASCII. Some systems
                      > assign some character, or character sequence, to signal EOF. Some
                      > historically popular choices have been:
                      >[/color]
                      Right.
                      [color=blue]
                      > ^Z SUB Who knows why, maybe because the last letter
                      > ^D EOT End of Transmission
                      > ^Y EM End of medium[/color]

                      Agree; also ^C ETX End of Text. Especially in the little if ever used
                      ASCII-ized version of bisync, X3.20-something IIRC.
                      [color=blue]
                      > ^[ ESC Escape[/color]

                      I've never seen any suggestion ESC was used by itself for EOF; it (and
                      to a somewhat lesser extent ^P DLE Data Link Escape) is very much used
                      as the prefix or introducer for a character or sequence representing
                      some function(s) according to many (mostly incompatible) schemes. I
                      could easily believe ESC-something for EOF, but not ESC alone.
                      [color=blue]
                      > ^\ FS File Separator
                      > ^] GS Group Separator
                      > ^^ RS Record Separator
                      > ^_ US Unit Separator
                      >[/color]
                      FS or maybe GS _should_ have been used for EOF, but I've never seen it
                      happen. The latter two probably shouldn't.
                      [color=blue]
                      > The most commonly used sequences are: CR SUB and CR EOT as seen at
                      > a keyboard. One system used ":EOF" at the immediate head of a line
                      > only, i.e. preceded by CR.
                      >[/color]
                      Quite a few Unix (derived) programs, I believe also Multics, and
                      several Internet protocols perhaps inspired by them, use "period aka
                      dot aka full-stop alone on a line" e.g. CR . CR

                      In the (ITU) X.3 terminal=user interface, X.29 IIRC, you would use one
                      character to escape to command mode and then issue a disconnect
                      command. In Telenet (the now-gone company, not telnet the protocol) I
                      think this was return atsign D return. Similarly in most(?) telnet
                      clients today there is some command-escape followable by a command
                      conventionally 'quit'.

                      One system I use (sometimes) uses squiggle-letter as a way to enter
                      control-letter, so ~ Y means ^Y means EOF. You could count this as a
                      sequence or just as an encoded character.

                      X.25 supports grouping of packets using the 'M'ore bit, and SNA
                      supports grouping of RUs at two levels: chains and brackets. But these
                      are for arbitrary binary data not necessarily characters, and whether
                      such groups correspond to files or not depends on the application.
                      [color=blue]
                      > Another generic means of signalling EOF from the keyboard is the
                      > power switch. This one may have undesirable side effects.[/color]

                      Or for a 'remote' terminal 'offline' or a modem/phone control.


                      - David.Thompson1 at worldnet.att.ne t

                      Comment

                      • Jordan Abel

                        #12
                        Re: How to fputc 'EOF' to a FILE stream.

                        On 2006-02-20, Dave Thompson <david.thompson 1@worldnet.att. net> wrote:[color=blue]
                        > Quite a few Unix (derived) programs, I believe also Multics, and
                        > several Internet protocols perhaps inspired by them, use "period aka
                        > dot aka full-stop alone on a line" e.g. CR . CR[/color]

                        However, this is not a suitable representation for text files as defined
                        in the C standard. Unless perhaps an _actual_ period by itself on a line
                        is represented by CR . SPACE CR or something like that [preservation of
                        trailing whitespace being implementation-defined]

                        Comment

                        • Ben Pfaff

                          #13
                          Re: How to fputc 'EOF' to a FILE stream.

                          Jordan Abel <random832@gmai l.com> writes:
                          [color=blue]
                          > On 2006-02-20, Dave Thompson <david.thompson 1@worldnet.att. net> wrote:[color=green]
                          >> Quite a few Unix (derived) programs, I believe also Multics, and
                          >> several Internet protocols perhaps inspired by them, use "period aka
                          >> dot aka full-stop alone on a line" e.g. CR . CR[/color]
                          >
                          > However, this is not a suitable representation for text files as defined
                          > in the C standard. Unless perhaps an _actual_ period by itself on a line
                          > is represented by CR . SPACE CR or something like that [preservation of
                          > trailing whitespace being implementation-defined][/color]

                          Often these protocols escape lines that begin with a dot by
                          adding a second dot in front of it. The stream implementation
                          could do filtering on output to add it and on input to remove it.
                          --
                          int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
                          \n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
                          );while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
                          );}return 0;}

                          Comment

                          Working...