gpt4 book ai didi

c - 为什么我无法输入在c 中打开的文件名?

转载 作者:行者123 更新时间:2023-11-30 19:24:16 25 4
gpt4 key购买 nike

我一直在尝试用我有限的知识调试这个程序,并将其与我使用相同方法检索要打开的文件名的其他程序进行比较。然而,似乎由于某种奇怪的原因,程序没有收到用户输入的文件名,有时会陷入某种难以捉摸的循环中。

我都用过: scanf("%s\n", 文件名);

以及: 获取(文件名);

(我知道 gets 是“危险的”,但这是一个不会分发的程序,它是大学水平类(class)中的作业)

这是 main() 函数和 getssn() 函数(它成功获取用户输入):

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define MAXS 19
#define MAXR 999

//structure defining a given client
typedef struct person {
unsigned int ssn, age, height, weight, income;
char name[MAXS+1], job[MAXS+1], religion[MAXS+1], major[MAXS+1], minor[MAXS+1], gender;
}PERSON;


//get and check for ssn validity
int getssn(){
int num;

printf("\nSSN: ");
scanf("%d", &num);

if(num<=99999999 || num>999999999){
printf("\nPlease input a valid SSN.\n");
return 0;
}
else
return num;
}

//read the specified file and check for the input ssn
int readfile(FILE *fptr, PERSON **rptr, int *count){
int v=0, i, j;
char n2[MAXS+1], b[2]=" ";

for(i=0; i<MAXR; i++){
j=i;

//read the file in chunks
if(fscanf(fptr, "%c\n%d\n%19s %19s\n%d\n%19s\n%d\n%19s\n%19s\n%d\n%d\n%19s\n\n",
&rptr[j]->gender, &rptr[j]->ssn, rptr[j]->name, n2, &rptr[j]->age,
rptr[j]->job, &rptr[j]->income, rptr[j]->major, rptr[j]->minor,
&rptr[j]->height, &rptr[j]->weight, rptr[j]->religion)==EOF)
i=MAXR;

//make first and last name one element
strcat(rptr[j]->name, b);
strcat(rptr[j]->name, n2);

//if we find a match, tell main the id
if(rptr[MAXR]->ssn==rptr[j]->ssn)
v=j;
}

//count how many clients we have
*count=j;

return v;
}

//commpare age and income
int cmpai(PERSON rec1, PERSON rec2){
int a=0, inc=0;

if(rec1.age<=(rec2.age+10) && rec1.age>=(rec2.age-10))
a=1;
if(rec1.income<=(rec2.income+10000) && rec1.income>=(rec2.income-10000))
inc=1;

if(a==1 && inc==1)
return 1;
else
return 0;


}

//compare hobbies
int cmph(PERSON rec1, PERSON rec2){
if(strcmp(rec1.major,rec2.major)==0 && strcmp(rec1.minor, rec2.minor)==0)
return 1;
else
return 0;
}

//compare weight, height, and religion
int cmpwhr(PERSON rec1, PERSON rec2){
int w=0, h=0, r=0;
double n1, n2;

n1=rec1.height;
n2=rec2.height;
if(n1<=(n2*1.1) && n1>=(n2*0.9))
h=1;

n1=rec1.weight;
n2=rec2.weight;
if(n1<=(n2*1.1) && n1>=(n2*0.9))
w=1;

if(strcmp(rec1.religion, rec2.religion)==0)
r=1;

if(r==1 && h==1 && w==1)
return 1;
else
return 0;
}

//sort the ids in ascending order by ssn for proper output
void sort(int *A, int count){
int i, j, temp;

for(i=0; i<count; i++)
for(j=0; j<count; j++)
if(A[i+1]<A[i]){
temp=A[i];
A[i]=A[i+1];
A[i+1]=temp;
}
}

//display the possible matches in ascending ssn order
void display(int matches[], PERSON rec[], int count){
int i;

for(i=0; i<count; i++){
if(matches[i]==rec[i].ssn)
printf("%s\n", rec[i].name);
}
}



int main(){
int valid=-1, i, counter=0, *c=&counter, id[MAXR-1], totalmatches;
char filename[MAXS];
FILE *fp;
PERSON record[MAXR+1], *rp[MAXR+1];

//get a ssn from the user
do{
record[MAXR].ssn=getssn();
}while(record[MAXR].ssn==0);

//get a filename
printf("Name of file of records: ");
scanf("%s", filename);
printf("%s", filename);

//open the file, if possible
if((fp=fopen(filename, "r"))==NULL)
perror(filename);
else{
printf("test");
for(i=0; i<=MAXR; i++){
rp[i]=&record[i];
id[i]=0;
}

//read the file, find the matching ssn
valid=readfile(fp, rp, c);

//check if the ssn is in the file, if not tell the user
if(valid<0){
printf("\nSSN %d is not found in file %s.\n", record[MAXR].ssn, filename);
return EXIT_FAILURE;
}
else {
//check for matches and count how many we have
for(i=0; i<counter; i++){
if(i!=valid)
if(record[valid].gender!= record[i].gender)
if(cmpai(record[valid], record[i])==1 || cmph(record[valid], record[i])==1 || cmpwhr(record[valid], record[i])==1){
id[i]=record[i].ssn;
totalmatches+=1;
}
}
//if we have matches sort them and display them, otherwise tell the user he has no match in this group
if(totalmatches>0){
sort(id, counter);
display(id, record, counter);
}
else
printf("\nNo matches.\n");
fclose(fp);
return EXIT_SUCCESS;
}
}
}

这是当前的输入(单引号)/输出:

run
[Switching to process 6956]
Running…

SSN: '111223333'
Name of file of records: 'clients.txt'
clients.txttest
Debugger stopped.
Program exited with status value:0.

最佳答案

你应该使用 getssn() 函数。您需要输入返回声明。

int getssn(){
int num;

printf("\nSSN: ");
scanf("%d", &num);
return num; // You MUST put this in.
}

这将改进此代码:

do{
record[MAXR].ssn=getssn();
}while(record[MAXR].ssn==0);

如果您忘记了返回值,那么 getssn() 可能会永远返回 0。

您还应该告诉我们您所看到的确切输出。您还应该将所有输出复制到您的问题中。

关于c - 为什么我无法输入在c 中打开的文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6100708/

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