无法从txt文件中读取数据
unable to read data from txt file
这是第一个将用户数据写入文件的代码
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fpr;
char ch='y';
struct student
{
int reg;
char name[50],remarks[10];
};
struct student a;
fpr=fopen("recstruct.txt","wb");
if(fpr==NULL)
{
printf("file does not exist");
}
while (ch=='y')
{
printf("enter the register number ,name and remarks:\n");
scanf("%d",&a.reg);
scanf("%s",&a.name);
scanf("%s",&a.remarks);
fwrite(&a,sizeof(a),1,fpr);
printf("do you want to add more record (y/n):\n");
ch=getche();
printf("\n\n");
}
fclose(fpr);
}
我给出的输入是:
1
charana
pass
y
2
charana
pass
运行正常,但文本文件以下列格式保存:
charana Ð>* ùjö pass charana
Ð>* ùjö pass
此处的第二个程序是从保存的文本文件中读取用户定义的寄存器编号,然后显示该特定寄存器编号的详细信息,例如名称和备注。
#include<stdio.h>
void main()
{
struct studentdata
{
int reg;
char name[50],remarks[50];
}a;
int reg,flag=0;
char name;
FILE * fpr;
fpr=fopen("recstruct.txt","rb");
if(fpr==NULL)
{
printf("file does not exist");
}
printf("enter the registration number:\n");
scanf("%d",®);
while(fread(&a,sizeof(a),1,fpr)>0 && flag==0)
{
if (a.reg==reg)
{
flag=1;
printf("record found\n");
printf("student name:%s registration number: %d\t remarks:%s",a.name,a.reg,a.remarks);
}
}
if (flag==0)
{
printf("record not found");
}
fclose(fpr);
}
但程序 returns 对于用户提供的除第一行数据以外的任何输入“未找到”
enter the registration number:
2
record not found
我不知道问题出在哪里,是第一个程序还是第二个..请帮我解决同样的问题。
您用于读取和写入的结构体大小不同:
struct student
{
int reg;
char name[50],remarks[10];
};
对比
struct studentdata
{
int reg;
char name[50],remarks[50];
}a;
具体来说,remarks
是不同的尺寸。
这是第一个将用户数据写入文件的代码
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fpr;
char ch='y';
struct student
{
int reg;
char name[50],remarks[10];
};
struct student a;
fpr=fopen("recstruct.txt","wb");
if(fpr==NULL)
{
printf("file does not exist");
}
while (ch=='y')
{
printf("enter the register number ,name and remarks:\n");
scanf("%d",&a.reg);
scanf("%s",&a.name);
scanf("%s",&a.remarks);
fwrite(&a,sizeof(a),1,fpr);
printf("do you want to add more record (y/n):\n");
ch=getche();
printf("\n\n");
}
fclose(fpr);
}
我给出的输入是:
1
charana
pass
y
2
charana
pass
运行正常,但文本文件以下列格式保存:
charana Ð>* ùjö pass charana
Ð>* ùjö pass
此处的第二个程序是从保存的文本文件中读取用户定义的寄存器编号,然后显示该特定寄存器编号的详细信息,例如名称和备注。
#include<stdio.h>
void main()
{
struct studentdata
{
int reg;
char name[50],remarks[50];
}a;
int reg,flag=0;
char name;
FILE * fpr;
fpr=fopen("recstruct.txt","rb");
if(fpr==NULL)
{
printf("file does not exist");
}
printf("enter the registration number:\n");
scanf("%d",®);
while(fread(&a,sizeof(a),1,fpr)>0 && flag==0)
{
if (a.reg==reg)
{
flag=1;
printf("record found\n");
printf("student name:%s registration number: %d\t remarks:%s",a.name,a.reg,a.remarks);
}
}
if (flag==0)
{
printf("record not found");
}
fclose(fpr);
}
但程序 returns 对于用户提供的除第一行数据以外的任何输入“未找到”
enter the registration number:
2
record not found
我不知道问题出在哪里,是第一个程序还是第二个..请帮我解决同样的问题。
您用于读取和写入的结构体大小不同:
struct student
{
int reg;
char name[50],remarks[10];
};
对比
struct studentdata
{
int reg;
char name[50],remarks[50];
}a;
具体来说,remarks
是不同的尺寸。