Re: Writing to a file

From: Ben Cartwright (europa@vt.edu)
Date: 09/03/00


At 05:04 PM 9/3/00 +0200, you wrote:
>> I want to write a buffer straight to a specific file. Simple?
>Ok, so someone enters an argument (arg) and you want to put it into a file..
>Lets see now..
>
>#define FILE_TO_SAVE "file_name.here"
>-snip-
>  static char 1_buf[MAX_INPUT_LENGTH];
>  FILE *fl; /* declare the file */
>-snip-
>
>       *1_buf = '\0'; /* Would rather free this if I could */
>       sprintf(1_buf, "%s", arg);
>       SEND_TO_Q(1_buf, d);
>       SEND_TO_Q("\r\nThank you.\r\n", d);
>       if (!(fl = fopen(FILE_TO_SAVE, "wb"))) { /* open the file */
>          log("SYSERR: opening file_name.here: %s", strerror(errno));
>       } else {
>         fprintf(fl, "%s\n", 1_buf); /* append the string */
>         fclose(fl); /* close the file */
>       }
>       STATE(d) = CON_MENU;
>       break;
> -snip-
> Standard disclaimer applies - mailer code, not my fault if stuff explodes etc. etc.
>
>Hopes this helps you out a bit..
>
>Welcor
>cruelworld.dune.net 9090

   First of all, "1_buf" is not a valid variable name in C.  Variables
must start with a letter or an underscore; numbers are allowed, but not
at the beginning.
   Second, in the above code, you'd want to pass "a" (append) to fopen,
not "wb" (binary write), or it will overwrite whatever used to be in
the file before.
   And finally, Blaize had the right idea.  Just reuse the code for the
idea/bug/typo commands in do_gen_write (act.other.c) by adding another
SCMD.  No need to reinvent the wheel. :)

--Ben C


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html  |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 04/11/01 PDT