The program runs fine till I create the file and enter records from structure, but when I try to search a record by using roll no, it crashes.
程序运行正常,直到我创建文件并从结构中输入记录,但是当我尝试使用roll no搜索记录时,它会崩溃。
I am using a structure called student to store data and then I am creating a text file and then I use a for loop to write data on the text file. Then I reopen the file and try to search a record using student roll no.
我正在使用一个名为student的结构来存储数据然后我创建一个文本文件,然后我使用for循环在文本文件上写入数据。然后我重新打开文件并尝试使用学生卷号搜索记录。
Program runs fine until I try to search the student roll no. It crashes right after I enter the student roll no to be searched.
程序运行正常,直到我尝试搜索学生卷号。在我输入要搜索的学生卷之后它立即崩溃。
Can anyone tell me what modification is needed to make the search work?
谁能告诉我需要哪些修改才能使搜索工作?
Below is my code:
以下是我的代码:
#include<stdio.h>
struct student {
int roll_no;
char name [80];
int age;
}st[30],s;
int main ()
{
int i,n;
char fname[80];
int search;
int found;
FILE *fp;
printf("\nEnter the file name : \n");
scanf("%s",fname);
fp=fopen(fname,"w");
if(fp==NULL)
{
printf("\nCannot create file :");
exit(0);
}
printf("\nNumber of students : \n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\nInformation for student#%d : \n\n",i+1);
printf("\nStudent roll number : \n" );
scanf("%d",&st[i].roll_no);
printf("\nStudent name: \n: ");
scanf("%s",st[i].name);
printf("\nStudent age : ");
scanf("%d", &st[i].age);
}
fprintf(fp, "\nStudent roll no\t\t Student name\t\t student age\t\t\n\n");
for(i=0;i<n;i++)
{
fprintf(fp, "\n%d \t\t %s \t\t %d \t\t", st[i].roll_no,st[i].name,st[i].age);
}
fclose(fp);
fp=fopen(fname,"r+t");
if(fp==NULL)
{
printf("\nCannot open file\n");
exit(0);
}
printf("\n\nStudent roll no to be searched : ");
found=0;
scanf("%d", search);
while(!(feof(fp)) && (found==0))
{
fscanf(fp,"%d,%s,%d",&s.roll_no,s.name,&s.age);
if(s.roll_no==search)
{
fseek(fp,-sizeof(struct student), SEEK_CUR);
printf("\nEnter new name : \n");
scanf("%s", s.name);
fprintf(fp, "\n%d \t\t %s \t\t %d \t\t", s.roll_no,s.name,s.age);
found=1;
}
}
if(found=0)
{
printf("\nStudent record doesn't exist \n");
}
fclose(fp);
return 0;
}
1 个解决方案
#1
2
In your code, you're missing an address-of operator in scanf()
, thereby passing an invalid type of argument. Basically
在你的代码中,你在scanf()中缺少一个address-of运算符,从而传递了一个无效的参数类型。基本上
scanf("%d", search);
should be
应该
scanf("%d", &search);
That said, it is always a good practice to size-limit the inputs for string, like, for an array defined like char fname[80];
, you should use
也就是说,对于字符串的输入进行大小限制总是一个好习惯,例如,对于像char fname [80]这样定义的数组,你应该使用
scanf("%79s",fname);
to avoid possible buffer overflow by excessively long input.
通过过长的输入来避免可能的缓冲区溢出。
Also, always check for the return value of scanf()
and family of functions to ensure their success.
此外,始终检查scanf()和函数系列的返回值,以确保它们的成功。
#1
2
In your code, you're missing an address-of operator in scanf()
, thereby passing an invalid type of argument. Basically
在你的代码中,你在scanf()中缺少一个address-of运算符,从而传递了一个无效的参数类型。基本上
scanf("%d", search);
should be
应该
scanf("%d", &search);
That said, it is always a good practice to size-limit the inputs for string, like, for an array defined like char fname[80];
, you should use
也就是说,对于字符串的输入进行大小限制总是一个好习惯,例如,对于像char fname [80]这样定义的数组,你应该使用
scanf("%79s",fname);
to avoid possible buffer overflow by excessively long input.
通过过长的输入来避免可能的缓冲区溢出。
Also, always check for the return value of scanf()
and family of functions to ensure their success.
此外,始终检查scanf()和函数系列的返回值,以确保它们的成功。