gpt4 book ai didi

c - 如何在c中将函数输入到文件中

转载 作者:行者123 更新时间:2023-11-30 21:43:31 25 4
gpt4 key购买 nike

我在代码中有这个函数,我试图输入 I/O 文件,但我似乎无法做到这一点。

void show_list(int whyeven[stuff], char *hatred[stuff])
{
for (int g = 0; g < stuff - 1; g++)
{
if (whyeven[g] < 10 || whyeven[g] == 0)
{
printf("%s - %d (*) you should buy more of this stuff\n\n",hatred[g], whyeven[g]);
}
else if (whyeven[g] > 10)
{
printf("%s - %d\n\n", hatred[g], whyeven[g]);
}
}
}

int main()
{
show_list(moarstuff, items);
return 0;
}

最佳答案

printf() 打印到 stdout。您需要 fopen() 该文件,然后使用 fprintf() 以及从 fopen() 返回的 FILE* 指针作为第一个参数。

/* Open the file for writing */
FILE* fp = fopen("filename.txt", "w");
/* Check for errors */
if (fp == NULL)
{
/* Notify the user of the respective error and exit */
fprintf(stderr, "%s\n", strerror(errno));
exit(1);
}
/* Write to the file */
fprintf(fp, "Hello!\n");
/* Close the file */
fclose(fp);

注意:您的问题相当不清楚,这个答案是基于我所能理解的。

关于c - 如何在c中将函数输入到文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41226930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com