Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printf doesn't return the correct number of bytes ?

Status
Not open for further replies.

ionutdinulescu

Programmer
Jun 11, 2002
59
0
0
GB
the code bellow:

n = fprintf(f, "%d", 4);

does not return 1, as I expected.
Do you know the cause, and how this can be fixed ?
 
ionutdinulescu

What return are you getting? When I execute that code, 1 is returned, as expected. If you are getting a (-1), then perhaps f has not been opened in a correct mode.

Rick
 
The file I'm using is NUL. The complete code is:
f = fopen("NUL", "wt")
n = fprintf(f, "%d", 4);
and is supposed to "write to nowhere" as I just need the number of charactes that have been written
I don't have VC on this computer, so I cannot tell you the exact result, but it's about 3 or something.
 
The following code works appropriately. I am not certain what is happening with you.

#include "stdafx.h"

int main(int argc, char* argv[])
{
FILE *inf;
int ii;
inf=fopen("NUL","wt");
if(inf){
ii=fprintf(inf,"%d",4);
fclose(inf);
}else ii=999;
printf("%d\n",ii);
return 0;
}

Rick
 
The following code:

FILE *f = fopen("NUL", "wt");
retval = fprintf(f, "%.2f, 2000.0);

returns 5 (so 2000. instead of 2000.00)

Does anyone know the cause for this ?



 
I have VC++ 6.0 SP5, fprintf(f, "%.2f", 2000.0) prints 2000.00 and returns 7 as expected (after obvious correction of missing quote in your snippet;).
What a strange RTL are you using?..
 
I'm using vc6.0 sp6. I really don't know what is happening. The return seems to be as writing to a binary file, although I open "NUL" as text.
 
I think that an appropriate thing to do here is simply change your fprintf(f,"%.2f",2000.0) function to sprintf(ss,"%.2f",2000.0), and then examine the contents of ss. Alternately, you could substitute a real file for NUL, and examine its contents. If you do, be sure to close the file, and place a \n at the end of your specification.

If you post that output, perhaps someone here can help.

My interest is piqued, if you find the solution, please let us know what it is.

Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top