同名输入/输出问题

发布于 2024-12-28 20:03:43 字数 1096 浏览 1 评论 0原文

好的,伙计们,我的程序还有一个问题。 我遇到了使用相同文件名(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);

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

Okay guys, I'm having one more issue with my program.
I'm running into an issue with using the same filename (KnightsBall."extension"). I don't know if it's something locally, or an issue I'll have when I move the code over. So can anyone help me with what thing I'm overlooking? The output and input have to both go to the same filename.(in) or (out) so no ways around. If I change the output file to knights.out it works. Any ideas?
Relevant segments of code:

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

and the part where the output is accessed:

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

Also on a completely unrelated side note, does anyone know how to close the command prompt after I scan in the values I need from the user? All help is much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长发绾君心 2025-01-04 20:03:43

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

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

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

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

Since you don't show the code where you check that you opened each file successfully, it is probable that you didn't open the output file. It is not clear why, but one possible problem is that the output file already exists and is read-only. Another might be that you don't have permission to create files in the directory.

I can't help but think you could use an array of strings to reduce the printing to:

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

Since there isn't a simple relation between the values, it is probably best to use a table rather than a calculation for the amounts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文