I'm trying to print my strings output into a separate file. The issue I'm running into right now is the fact that my code carries a function for a set of strings that adds dashed lines underneath my columns (purely cosmetics). How do I call this function inside my fprintf code?
#include <stdio.h>
/* function for the dash-line separators*/
void
dashes (void)
{
printf (" ---- ----- -------------------- --------------\n");
}
/* end of function definition */
/* main program */
#include <stdio.h>
#include <string.h>
int
main (void)
{
FILE *data_File;
FILE *lake_File;
FILE *beach_File;
FILE *ecoli_Report;
char fileName[10], lake_Table[15],beach_Table[15]; /*.txt file names */
char province[30] = ""; /*variable for the file Lake Table.txt*/
char beach[20]="",beach1[20]; /*variable for the file Beach Table.txt*/
char decision[15] = "CLOSE BEACH";
int lake_data=0,lake_x=0, beach_x=0, nr_tests=0; /* variables for the file july08.txt */
int province_data=0,prv_x=0; /* variables for the file Lake Table.txt */
int beach_data=0,bch_x=0; /* variables for the file Beach Table.txt*/
int j;
double sum, avg_x, ecoli_lvl;
printf ("Which month would you like a summary of? \nType month followed by date (i.e: july05): ");
gets(fileName);
/*Opening the files needed for the program*/
data_File = fopen (fileName, "r");
lake_File = fopen ("Lake Table.txt", "r");
beach_File = fopen ("Beach Table.txt", "r");
ecoli_Report = fopen ("Lake's Ecoli Levels.txt", "w");
fprintf (ecoli_Report,"\n Lake Beach Average E-Coli Level Recommendation\n");
fprintf (ecoli_Report,"%c",dashes());
If you were to recode your function as follows:
then you could use it in either way. Calling
dashes()
would still output the string to standard output followed by a newline, and that would be equivalent to:Alternatively, you could perform arbitrary actions (a) with the string returned from
strdashes()
(other than trying to change it as a string literal of course):(a) Such as write it to a different file handle, get the length of it with
strlen()
, make a copy of it withstrcpy()
where you might want to replace all-
characters with=
, really a wide variety of possibilities.You need to change your dashes function to take a pointer to the file stream you want used for output. Then use fprintf within the function instead of printf.
Alternatively, you could have dashes return the string (
char *
) and then use the fprintf - note you want%s
not%c
as currently coded.dashes()
is void returning function how will you get this line?If you need to print the line in the file, make the prototype and call like this,
Remove this line.
And change that calling into like this,
Or else simply do like this,
Add a FILE parameter to your function and pass the file handle to it and use fprintf inside the function.
Alternatively, you can have dashes return a character array instead of void.