Sign in to follow this  
personwholives

fprintf is buggy

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this