Calling a function within fprintf syntax in C lang

2019-07-29 05:25发布

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());

4条回答
一夜七次
2楼-- · 2019-07-29 05:51

If you were to recode your function as follows:

char *strdashes (void) {
    return "  ----           -----        --------------------     --------------";
}
void dashes (void) {
    puts (strdashes());
}

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:

printf ("%s\n", strdashes());

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):

fprintf (errorLog, "%s: %s\n", datetime(), strdashes());

(a) Such as write it to a different file handle, get the length of it with strlen(), make a copy of it with strcpy() where you might want to replace all - characters with =, really a wide variety of possibilities.

查看更多
祖国的老花朵
3楼-- · 2019-07-29 05:55

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.

查看更多
甜甜的少女心
4楼-- · 2019-07-29 05:57

dashes() is void returning function how will you get this line?

 fprintf (ecoli_Report,"%c",dashes());

If you need to print the line in the file, make the prototype and call like this,

 void dashes(FILE *fp){
    fprintf(fp,"------------------\n");
 }

Remove this line.

 fprintf (ecoli_Report,"%c",dashes());

And change that calling into like this,

 dashes(ecoli_Report);

Or else simply do like this,

 fprintf(ecoli_Report,"----------------");
查看更多
唯我独甜
5楼-- · 2019-07-29 06:04

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.

查看更多
登录 后发表回答