gpt4 book ai didi

c - 如何将用户输入连续写入文件? C语言

转载 作者:行者123 更新时间:2023-11-30 15:44:50 25 4
gpt4 key购买 nike

我正在编写一个必须读取用户输入(姓氏、名字和分数)并将此信息写入文本文件的程序。我创建了一个循环,提示输入并为每个输入权限设置标志。当收集所有输入并且用户不想添加任何其他内容时,循环应该结束。

问题是我设置了变量来保存名称,但我需要一种方法来在循环的下一次迭代中重用这些变量。因此,循环开始,收集用户输入,写入文件,然后它应该“清除”新迭代的变量。

用户应该能够在文件中插入尽可能多的数据。但现在,我只能添加一组数据:(姓氏、名字和分数)。

void enter_data(void)
{

/* create file */
FILE *fp;

/*variables*/
char answer;
int flag1 = 0;
int flag2 = 0;
int flag3 = 0;
int score = 0;
int test;
char leave[SIZE] = "exit";
char input_lname[SIZENAME];
char input_fname[SIZENAME];
char input_score[5];
char input_answer[5];
char lname[SIZENAME];
char fname[SIZENAME];




/*check if file was created */
if((fp = fopen("students.dat", "ab+")) == 0){
perror("fopen");
}


/* start of interaction */
while(!(flag1 && flag2 && flag3))
{
flag1 = 0;
flag2 = 0;
flag3 = 0;

/* ask for last name */
while(flag1 != 1)
{
printf("Enter last name:\n");

if(!fgets(input_lname, SIZENAME, stdin))
{
clearerr(stdin);
break;
}
/* validate last name. if not digit... */
if(!sscanf(input_lname, "%d", &test) == 1)

/*put first word in lname */
sscanf(input_lname, "%16s", lname);

if(strcmp(input_lname, leave) != 0)
{
flag1 = 1;
break;
}
}


/* ask for first name */
while(flag2 != 1)
{
printf("Enter first name:\n");

if(!fgets(input_fname, SIZENAME, stdin))
{
clearerr(stdin);
break;
}
/* validate first name. if not digit... * */
if(!sscanf(input_fname, "%d", &test ) == 1)
{
/*put first word in fname */
sscanf(input_fname, "%16s", fname);

flag2 = 1;
break;
}
}


/* ask for score */
while(flag3 != 1)
{
printf("Enter score:\n");

if(!fgets(input_score, SIZENAME, stdin))
{
clearerr(stdin);
break;
}

/* validate last name */
if(sscanf(input_score, "%3d", &score) == 1)
{
if((score >= 0) && (score <= 100))
{
flag3 = 1;
break;
} else if (score == -1)
break;
}
}

/* store record in file */
if((flag1 == 1) && (flag2 == 1) && (flag3 == 1))
{
printf("r %16s, %16s, %5d", lname, fname, score);
fprintf(fp, "%16s %16s %5d", lname, fname, score);


}

/* ask for another student */
printf("Would you like to enter data for another student?\n");
if(!fgets(input_answer, SIZE, stdin))
{
clearerr(stdin);
break;
}
if(sscanf(input_answer, "%s", &answer) == 1)
{
if(answer == 'y')
{
flag1 = 0;
flag2 = 0;
flag3 = 0;
continue;
} else
break;
}

flag1 = 0;
flag2 = 0;
flag3 = 0;

fclose(fp);
/* finish asking */

}


}

最佳答案

   struct Record {
/*variables*/
char answer;
int flag1;
int flag2;
int flag3;
int score;
int test;
char leave[SIZE];
char input_lname[SIZENAME];
char input_fname[SIZENAME];
char input_score[5];
char input_answer[5];
char lname[SIZENAME];
char fname[SIZENAME];
}

while ( /* condition */ ) {
// Create a record to keep the user inputs.
Record oneRecord;
oneRecord.flag1 = 1;
oneRecord.flag2 = 2;
....

// Write to file or other else.
}

The 'struct' syntax可能对你有帮助。

关于c - 如何将用户输入连续写入文件? C语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19312247/

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