fprintf用於打印文件中的內容,而不是stdout控製台。
int fprintf(FILE *fptr, const char *str, ...);
例:
#include<stdio.h>
int main()
{
int i, n=2;
char str[50];
//open file sample.txt in write mode
FILE *fptr = fopen("sample.txt", "w");
if (fptr == NULL)
{
printf("Could not open file");
return 0;
}
for (i=0; i<n; i++)
{
puts("Enter a name");
gets(str);
fprintf(fptr,"%d.%s\n", i, str);
}
fclose(fptr);
return 0;
}
Input:GeeksforGeeks
GeeksQuiz
Output: sample.txt file now having output as
0. GeeksforGeeks
1. GeeksQuiz
相關用法
注:本文由純淨天空篩選整理自 fprintf() in C。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
