Re: [NEWBIE][CODE] Error writing to file ... :(

From: Juliano Ravasi Ferraz (jferraz@linkway.com.br)
Date: 06/27/02


Justin Adler wrote:
> void append_to_file (char *line, char *filename) {
> ...
> fprintf (file, line);         // ***** CRASH HERE

Please, never do this anymore. The second argument of fprintf IS NOT a
char pointer pointing to the string to write. It is a FORMAT STRING
saying HOW the FOLLOWING arguments will be written. Unless you know
precisely the contents of the second argument (mostly a character string
constant directely written there), you should not do this.

It's ok to use things like
    fprintf(file, "hi");
because the format string is in front of your eyes, and you see that
there is no "%" sign in it, so it will print exactely what you want.

You should use either:
    fprintf(file, "%s", line);
or
    fputs(line, file);  /* preferably IMO */

I should also suggest you to use const on both parameters of your
function (append_to_file), and to remove the line saying "return;", once
it is unecessary.

--
Juliano Ravasi Ferraz <jferraz@linkway.com.br>
Linux User #164664

The guy who writes all those bumper stickers HATES New York.

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/25/03 PDT