[英]How to print function into different files in c programming
I have defined the following function in the header region, 我在标题区域定义了以下函数,
void printArray(float list[]){ /* function decleration for printing array elements */
int iii;
for (iii = 0; iii < length;iii++){
printf("%lf\n", list[iii]);
}
printf("\n");
}
To print this I just write printArray(x);
为了打印这个,我只写了
printArray(x);
where I have an array x[iii]=iii;
我有一个数组
x[iii]=iii;
Throughout the code I want to print this into different files. 在整个代码中,我想将其打印到不同的文件中。 So early on I want this to print to
FILE *File_First_Array_Printing;
因此,我很早就希望将其打印到
FILE *File_First_Array_Printing;
with an "initialised variables" text file, while later I want it to print to FILE *File_Fourth_Array_Printing;
带有“初始化变量”文本文件,稍后我希望它打印到
FILE *File_Fourth_Array_Printing;
with a "final variables" text file. 与“最终变量”文本文件。
Normally when I print into files I would do something like this, 通常,当我打印到文件中时,我会做这样的事情,
File_First_Array_Printing=fopen("Initialised-variables.txt","w");
fprintf(File_First_Array_Printing,"initialised array x: %f\n",printArray(x));
/* printing the initialised arrays */
fclose(File_First_Array_Printing);
But it doesn't seem to like that! 但这似乎不喜欢它! Please could you advise?
请您指教?
You should pass the FILE *
to your printArray
function and use that inside of the function to call fprintf
: 您应该将
FILE *
传递给printArray
函数,并在函数内部使用该函数来调用fprintf
:
void printArray(FILE *fp, float list[]){
int iii;
for (iii = 0; iii < length;iii++){
fprintf(fp, "%lf\n", list[iii]);
}
fprintf(fp, "\n");
}
Then call it like this: 然后这样称呼它:
File_First_Array_Printing=fopen("Initialised-variables.txt","w");
if (!File_First_Array_Printing) {
perror("open of Initialised-variables.txt failed");
} else {
printArray(File_First_Array_Printing, x);
fclose(File_First_Array_Printing);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.