Re: Writing to a file

From: Peter Ajamian (
Date: 09/04/00

Shane Lee wrote:

> However, I specifically said that the code I was using was a mockup of
> my problem area, not the actual code.

When you post a mockup, expect to have the mockup criticized, if you
want to find the problem in your actual code, then post the actual code.

>  You also said that I would want to pass "a", but I also wrote that I
> would be deleting the file as soon as it was wrote to. I use a perl script
> for this. Thus, I have to 'touch' the file before I write to it every
> time.  So "wb" should work fine.

My understanding is you're writing a text buffer, so why are you opening
a binary file? In most Unix's the "b" is ambiguous, it does the exact
same thing with ot without it, but for the sake of portability, it is a
good idea to use it only when writing a binary file.

Also, you need not delete and touch a new file, fopen will create the
new file if it does not exist, and will also truncate it (or delete the
contents) if it does exist.

>  Next, the idea of using SUB_CMD's hadn't occured to me, since this is
> all happening in the nanny() proc (hence the reference to SEND_TO_Q).
> I'm not too sure, but I do believe that a SUB_CMD would be wasted
> there.

Hrmmm, I have no idea where anyone got the idea that you were doing from
nanny, all I see in your original post is that you want code to dump a
text buffer (a character array) to a file.

Anyways, here is my answer to what I percieve to be your original

void dump_to_file(const char *filename, const char *buffer)
  FILE *fp;

  if (!(fp = fopen(filename, "w")))

  fputs(buffer, fp);


There it is, doesn't get much simpler or straightforward than that.

Regards, Peter

     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  |

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