-
fprintf
I have trouble fprintf to output.txt file..
FILE* out;
int number, result;
number = 291;
result = 15;
d = number ^ result; /* OR to get answer to store to output file */
fprintf((out, "%d", d));
when compilling, I get an error... msg " error fprintf: too few arguments for call" and "out: undeclared identifier" can someone tell what is wrong to the code? I wrote this code in C.
-
( and (( and ((( and (((( and ((((( might be interchangeable in math, but not in C.
-
Why is there 2 sets of parenthesis in the fprintf() call?
-
FILE* out;
int number, result;
number = 291;
result = 15;
d = number ^ result; /* OR to get answer to store to output file */
fprintf(out, "%d", d);
Ok, I remove extra ( ) like you guys suggested. I recompile, then I still get this error
"out undeclared identifier"
the FILE pointer is already declared above, so I don't see why compiler complains...
What can be wrong? I did re-check to see if output.txt is already exist in my work directory, the output.txt is there.
What can be wrong? need help!!
-
You'd have to post actual code for us to be certain, but apparently out was defined at some other scope. (Perhaps you are trying to print from a function, which will know nothing of variables in main?)
-
open attached file for code
If return 1, then I want "d" write to output.txt file.. or else false return back function caller
Just look at "int replaceNibble (int number, int nibble, int value, int newVal)" function difinition only and fprintf(out, "%d", d);
I kept get compilling error "out undeclared identifier"..and don't know why...
need your expert and help on this guys...
wrote code in C.
-
Well, look at your code:
Code:
int replaceNibble (int number, int nibble, int value, int newVal)
{
if (d == value)
{
int result, d;
result = newVal;
result = result << 12; /* new value that replace; now write to output.txt file */
d = number ^ result; /* OR to get answer to store to output file */
fprintf(out, "%d", d);
printf("replace and has new number %d\n", d);
return 1;
}
return 0;
}
There is indeed no declaration of a variable called "out" anywhere in that code. If you want to use the out that you have in main, then you need to pass it in to the function with everything else.
-
int replaceNibble (int number, int nibble, int value, int newVal, FILE* out)
After I passed " FILE* out" in the function, then the code works like a charm. Thanks you for that great support.
Is good ideal to put "FILE* out" as global ? I try think a good reason why I want to use declare "FILE* out" as a global? Maybe someone can give me insight global is a good choice here? Global is dangerous, so I try to minimize the use of global.
int replaceNibble (int number, int nibble, int value, int newVal, FILE* out)
{
if (d == value)
{
int result, d;
result = newVal;
result = result << 12; /* new value that replace; now write to output.txt file */
d = number ^ result; /* OR to get answer to store to output file */
fprintf(out, "%d", d); /* write to output.txt file */
printf("replace and has new number %d\n", d);
return 1;
}
return 0;
}
Thank you, Tabstop...and I should figure that out...but I kept refer to the error to see what mistake I have made... I don't see any mistake, so I kept ask myself why.... Thanks for pointing that out...
-
Globals aren't dangerous. You just generally don't want to use a crapload of them because they're hard to keep track of once your programs get anywhere up in size.
Quzah.