gpt4 book ai didi

c - 同名输入/输出问题

转载 作者:行者123 更新时间:2023-11-30 18:02:42 25 4
gpt4 key购买 nike

好的,大家好,我的程序还有一个问题。我遇到了使用相同文件名(KnightsBall.“扩展名”)的问题。我不知道这是本地的问题,还是我将代码移过来时会遇到的问题。那么任何人都可以帮助我解决我所忽略的事情吗?输出和输入必须都转到相同的文件名。(in)或(out)所以没有办法。如果我将输出文件更改为knights.out,它就可以工作。有任何想法吗?相关代码段:

FILE *fr; 
FILE *fo;

typedef struct KnightsBallLottoPlayer
{
char firstName[20];
char lastName[20];
int numbers[6];
} KBLottoPlayer;


int main()
{
KBLottoPlayer *temp;
int numPlays=0;
//What file to read from
fr = fopen("KnightsBall.in", "r");
//What file to read to
fo = fopen("KnightsBall.out", "w");

以及访问输出的部分:

if(match==3)
fprintf(fo,"%s %s matched %d numbers and won $10.\n", temp[r].firstName, temp[r].lastName, match);
if(match==4)
fprintf(fo,"%s %s matched %d numbers and won $100.\n", temp[r].firstName, temp[r].lastName, match);
if(match==5)
fprintf(fo,"%s %s matched %d numbers and won $10000.\n", temp[r].firstName, temp[r].lastName, match);
if(match==6)
fprintf(fo,"%s %s matched %d numbers and won $1000000.\n", temp[r].firstName, temp[r].lastName, match);

另外,在完全不相关的旁注中,有谁知道在我扫描用户需要的值后如何关闭命令提示符?非常感谢所有帮助。

最佳答案

由于您没有显示检查是否成功打开每个文件的代码,因此您可能没有打开输出文件。目前尚不清楚原因,但一个可能的问题是输出文件已经存在并且是只读的。另一种可能是您没有在该目录中创建文件的权限。

我忍不住认为你可以使用字符串数组来将打印减少到:

static const char *amounts[] = { "", "", "", "10", "100", "10000", "1000000" };
if (match >= 3 && match <= 6)
fprintf(fo, "%s %s matched %d numbers and won $%s.\n",
temp[r].firstName, temp[r].lastName, match, amounts[match]);

由于值之间不存在简单的关系,因此最好使用表格而不是计算金额。

关于c - 同名输入/输出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8981891/

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