fprintf is buggy

Started by
6 comments, last by personwholives 19 years, 7 months ago
i am using straight c for a project and i am attempting to write a series of integers to a file usign fprintf. the file is already opened (and confirmed to be present) and puts() works fine with it. however, when i use fprintf(file, "%n\n" number) i get an access violation from within the bowels of fprintf. i don't understand it. the same result is acheived with sprintf() and printf(). Any guesses would be greatly appreciated. Thanks. personwholives

Stupid details.....always get in the way....it's time to write the dwim(x) function ("do what i mean to")
Advertisement
just replace %n by %d
and read the manual page about [f|s|sn]printf ...
--flurehttp://flure.free.fr
What compiler you are using, what is the definition of 'number', and what are the lines of code concerned with the 'file' variable?

You missed a comma before 'number' in your post btw.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by flure
just replace %n by %d
Exactly. %n interprets its parameter as a pointer to an integer and sets the pointed-to value to the number of characters written so far:
int six = 6, nine = 9;printf("%i * %i =%n", six, nine, &nine);printf(" %i", six*nine);
fprintf takes 3 arguements:

fprintf(filepointer, control string, variables.....);

in the control string you can have differnt formats that corespond to the variables after the control string.

%d = integer
%f = float
%lf = double

there are more, but im too lazy.

basically, the call you should have been making looks like this:

fprintf(file, "%d \n", number);

cheers
General hint: Whenever you suspect there is a bug in your compiler or the standard library, there is not, you are doing something wrong. Of course this is not the case 100% of cases, but it is in 99.999923% at the very least.
fprintf is not buggy.

have you read the manual?
sorry, i figured it out shortly after i made this post, but my stupid school dropped the internet connection for most of the night.

Stupid details.....always get in the way....it's time to write the dwim(x) function ("do what i mean to")

This topic is closed to new replies.

Advertisement