fopen

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Cunningham

    fopen

    I have a question about fopen. I want to append text to a text file
    already written. I think this is what I need to do. If I am correct fopen's
    second parameter takes only 2 letters. I am using wt. To append text would I
    need another call to fopen if my text file isn't already existing consisting
    of wa or a+ ? How can I check if an existing text file exists that has been
    created before ?

    Bill


  • Walter Roberson

    #2
    Re: fopen

    In article <VgA5k.40538$lE 3.23882@trnddc0 5>,
    Bill Cunningham <nospam@nspam.c omwrote:
    I have a question about fopen. I want to append text to a text file
    >already written. I think this is what I need to do. If I am correct fopen's
    >second parameter takes only 2 letters.
    No, there are well-defined three-character sequences.
    >I am using wt.
    The standard does not define the meaning of 't', only of 'b' (binary).
    Text is assumed if binary is not specified.
    To append text would I
    >need another call to fopen if my text file isn't already existing consisting
    >of wa or a+ ? How can I check if an existing text file exists that has been
    >created before ?
    fopen() with 'a'. ftell() your current position, fseek() to the
    beginning of the file, and ftell() again. If the first ftell()
    result is the same as the second ftell() then you started out at
    the beginning of the file and so there was no existing text.

    (Note: If I recall correctly, in some systems when using text streams,
    the initial file position is officially considered to be "one past"
    the end of file, rather than right -at- the end of file. The writes
    still work the same way, but if my memory is correct on this point,
    then ftell() on a newly-created text file might not return the
    same value as ftell() from the "beginning" of the same file.)
    --
    "Product of a myriad various minds and contending tongues, compact of
    obscure and minute association, a language has its own abundant and
    often recondite laws, in the habitual and summary recognition of
    which scholarship consists." -- Walter Pater

    Comment

    • Serve Lau

      #3
      Re: fopen


      "Bill Cunningham" <nospam@nspam.c omschreef in bericht
      news:VgA5k.4053 8$lE3.23882@trn ddc05...
      I have a question about fopen. I want to append text to a text file
      already written. I think this is what I need to do. If I am correct
      fopen's second parameter takes only 2 letters. I am using wt. To append
      text would I need another call to fopen if my text file isn't already
      existing consisting of wa or a+ ?
      read your favorite C book and try it out, its in there no matter if its a
      good or bad book

      Comment

      • vippstar@gmail.com

        #4
        Re: fopen

        On Jun 16, 11:44 pm, "Bill Cunningham" <nos...@nspam.c omwrote:
        I have a question about fopen. I want to append text to a text file
        already written. I think this is what I need to do. If I am correct fopen's
        second parameter takes only 2 letters. I am using wt.
        People have already told you numerous times 't' is non-standard.
        How many more times do you need to be told 't' is non-standard?
        Here, once more: 't' is non-standard.



        Just in case: 't' *is* *non*-standard.

        Comment

        • Bill Cunningham

          #5
          Re: fopen


          "Serve Lau" <nihao@qinqin.c omwrote in message
          news:e8de8$4856 df32$541fc2ec$3 0325@cache5.til bu1.nb.home.nl. ..
          read your favorite C book and try it out, its in there no matter if its a
          good or bad book
          Already checked it. I will check out the mentioned functions. Always
          wondered what fpos did. I'll check ftell and such.

          Bill


          Comment

          • Bill Cunningham

            #6
            Re: fopen


            "Walter Roberson" <roberson@ibd.n rc-cnrc.gc.cawrote in message
            news:g36kmr$l0s $1@canopus.cc.u manitoba.ca...
            The standard does not define the meaning of 't', only of 'b' (binary).
            Text is assumed if binary is not specified.
            [snip]

            I heard something about Microsoft and the 't'. If it isn't standard why use
            it. What does M$ have to do with this anyway? I just won't use it anymore. I
            will work with the functions you mentioned thanks very much.

            Bill


            Comment

            • Bill Cunningham

              #7
              Re: fopen


              "Walter Roberson" <roberson@ibd.n rc-cnrc.gc.cawrote in message
              news:g36kmr$l0s $1@canopus.cc.u manitoba.ca...

              I've tried this. Every new number overwites the last. Here is the code I can
              see I don't know how to use fseek.

              #include <stdio.h>
              #include <stdlib.h>

              int
              main (int argc, char *argv[])
              {
              if (argc != 2)
              {
              puts ("usage error");
              exit (EXIT_FAILURE);
              }
              double x;
              x = strtod (argv[1], NULL);
              FILE *fp;
              fp = fopen ("data", "wa");
              fprintf(fp,"%.2 f\n",x);
              ftell(fp);
              fseek(fp,0,1);
              ftell(fp);
              fclose (fp);
              return 0;
              }

              What did I do wrong?

              Bill


              Comment

              • Bill Cunningham

                #8
                Re: fopen


                <vippstar@gmail .comwrote in message
                news:2ae5b151-a56c-4395-bd3b-806ca28b892f@8g 2000hse.googleg roups.com...
                People have already told you numerous times 't' is non-standard.
                How many more times do you need to be told 't' is non-standard?
                Here, once more: 't' is non-standard.
                >
                >
                >
                Just in case: 't' *is* *non*-standard.
                Hum don't remember talking about it. I remember something mentioned
                about M$ and t. Well no matter. I can see I can count on you for help that
                is not needed. Have I not made to your killfile yet? What can I do to help?
                You're in mine.

                Bill


                Comment

                • Bill Cunningham

                  #9
                  Re: fopen


                  "Bill Cunningham" <nospam@nspam.c omwrote in message
                  news:66D5k.2094 8$3j2.17956@trn ddc03...
                  >
                  "Serve Lau" <nihao@qinqin.c omwrote in message
                  news:e8de8$4856 df32$541fc2ec$3 0325@cache5.til bu1.nb.home.nl. ..
                  You know my book doesn't give return values for these functions for
                  error checking. What about ftell and fseek do they return -1 or non zero
                  when successful ?

                  Bill


                  Comment

                  • Richard Heathfield

                    #10
                    Re: fopen

                    Bill Cunningham said:

                    <snip>
                    Have I not made to your killfile yet? What can I do
                    to help? You're in mine.
                    If he really were in your killfile, you would not have seen his article.

                    --
                    Richard Heathfield <http://www.cpax.org.uk >
                    Email: -http://www. +rjh@
                    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                    "Usenet is a strange place" - dmr 29 July 1999

                    Comment

                    • Bill Cunningham

                      #11
                      Re: fopen


                      "Richard Heathfield" <rjh@see.sig.in validwrote in message
                      news:_uOdnf1Ika L5gsrVnZ2dneKdn ZydnZ2d@bt.com. ..
                      If he really were in your killfile, you would not have seen his article.
                      >
                      I use OE. There's no killfile there. I just don't read posts from certain
                      persons. I know agent has a killfile. There comes a time to not read some
                      posts. A "killfile".

                      Bill


                      Comment

                      • Richard Heathfield

                        #12
                        Re: fopen

                        Bill Cunningham said:
                        >
                        "Bill Cunningham" <nospam@nspam.c omwrote in message
                        news:66D5k.2094 8$3j2.17956@trn ddc03...
                        >>
                        >"Serve Lau" <nihao@qinqin.c omwrote in message
                        >news:e8de8$485 6df32$541fc2ec$ 30325@cache5.ti lbu1.nb.home.nl ...
                        >
                        You know my book doesn't give return values for these functions for
                        error checking.
                        In message <3d38f60e_3@cor p.newsgroups.co m(posted on 20 July 2002) you
                        wrote:

                        "This little K&R2 book is giving me the devil of a time."

                        It is not unreasonable to deduce from that statement that you have had a
                        copy of K&R2 since at least 2002.

                        Despite your claim, K&R2 *does* document the return value semantics for
                        fopen, freopen, fflush, fclose, fprintf, fscanf, fgetc, fgets, fputc,
                        fputs, fread, fwrite, fseek, ftell, fgetpos, and fsetpos - on pp242-248.
                        What about ftell and fseek do they return -1 or non zero
                        when successful ?
                        Read the book.

                        --
                        Richard Heathfield <http://www.cpax.org.uk >
                        Email: -http://www. +rjh@
                        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                        "Usenet is a strange place" - dmr 29 July 1999

                        Comment

                        • Richard Heathfield

                          #13
                          Re: fopen

                          Bill Cunningham said:
                          >
                          "Richard Heathfield" <rjh@see.sig.in validwrote in message
                          news:_uOdnf1Ika L5gsrVnZ2dneKdn ZydnZ2d@bt.com. ..
                          >If he really were in your killfile, you would not have seen his article.
                          >>
                          I use OE. There's no killfile there.
                          Then he can't be in one, can he?
                          I just don't read posts from certain persons.
                          If that were true, you would not have read his article, so there would have
                          been no cause to respond to it.

                          --
                          Richard Heathfield <http://www.cpax.org.uk >
                          Email: -http://www. +rjh@
                          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                          "Usenet is a strange place" - dmr 29 July 1999

                          Comment

                          • Richard

                            #14
                            Re: fopen

                            "Bill Cunningham" <nospam@nspam.c omwrites:
                            I have a question about fopen. I want to append text to a text file
                            already written. I think this is what I need to do. If I am correct fopen's
                            second parameter takes only 2 letters. I am using wt. To append text would I
                            need another call to fopen if my text file isn't already existing consisting
                            of wa or a+ ? How can I check if an existing text file exists that has been
                            created before ?
                            >
                            Bill
                            Try reading the man page Bill. I hear it helps.

                            Comment

                            • Richard

                              #15
                              Re: fopen

                              "Bill Cunningham" <nospam@nspam.c omwrites:
                              "Walter Roberson" <roberson@ibd.n rc-cnrc.gc.cawrote in message
                              news:g36kmr$l0s $1@canopus.cc.u manitoba.ca...
                              >
                              >The standard does not define the meaning of 't', only of 'b' (binary).
                              >Text is assumed if binary is not specified.
                              [snip]
                              >
                              I heard something about Microsoft and the 't'. If it isn't standard why use
                              it. What does M$ have to do with this anyway? I just won't use it anymore. I
                              will work with the functions you mentioned thanks very much.
                              >
                              Bill
                              Alright, give it up Kenny. It's not funny anymore :-)

                              Comment

                              Working...