Im stuck when trying to write to my file within my subprogram.
void new_page(float *a, float *b, float *c, int *d){
fprintf(results,"\nPage Totals: %f\t%f\t%f\t%d", *a,*b,*c,*d);
}
I get a warning saying
Warning: incompatible implicit declaration of built-in function 'fprinf' [enabled by default]
“error: ‘results’ undeclared (first use in this function)”
in main fprintf works fine, its just when it comes to the subprogram/function it wont work. from my understanding it thinks that results is undeclared, so do i have to pass the name or location of the file to make it work?
Unless
resultsis global, it is undefined.From the looks of it, you have opened a file (stream) as
resultsin main. If so it only exists in the scope of main.Pass it along to the function. Do not recommend to make it global.
It would look like:
Also; if you have this function in a separate file then
main(), where, as you sayfprintfworks – you have to includestdio.hin that file as well.Edit: Sounds like you are doing something wrong with that function signature and prototype etc.
As a simple example (without needed checks on fopen etc):
Also; you say your compiler is not complaining. Are you using extensive warnings? If not you should. Ie GCC:
I usually also add
-std=c89, or if needed,-std=c99.Then run it wit valgrind:
These two steps will give you lots of hints and clues to where things could be wrong.