【发布时间】:2016-10-28 02:31:46
【问题描述】:
这段代码是一个关于文件 I/O 的小学习经验,我还不能完全完成:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
FILE *fP;
char so_name[64];
char user_name[64];
int number;
char continue_button;
time_t t;
system("TITLE Love-0-meter");
system("COLOR 4D");
srand((unsigned) time(&t));
number = (rand() %100);
printf("Hi and welcome to the love-o-meter, please fill in the details and see how much you and your SO"
" love each other!\n\nHave you used this program before?(Y/N)");
do
{
scanf(" %c", &continue_button);
}while(continue_button != 'n' && continue_button != 'N' && continue_button != 'Y' && continue_button != 'y');
system("CLS");
printf("Enter your SO's name: ");
scanf(" %s", so_name);
printf("Enter your name: ");
scanf(" %s", user_name);
printf("\n%s and %s, you love each other %d%%", so_name, user_name, number);
if(continue_button == 'N' || continue_button == 'n')
{
fP = fopen("Love-o-meter.txt", "w");
}
else
{
fP = fopen("Love-o-meter.txt", "a");
}
fprintf(fP, "%s %s %i\n", so_name, user_name, number);
fclose(fP);
return 0;
}
基本上,用户必须在其中输入 2 个名称并与随机数一起保存在 .txt 文件中。但是在保存名称之前,程序必须检查它们是否以前以相同的顺序使用过并且已经在文件中。如果之前确实使用过,则程序不得生成新号码,而是将保存的号码与 2 个名称一起使用并在屏幕上打印出来。
我已经阅读了一整天,但我只是不知道如何以及如何实现以使程序能够读取以前保存的名称和数字并在找到匹配项时使用它们。
【问题讨论】:
标签: c string file search integer