C 如何从数据数组的结构中搜索特定结构并使用fread读取?
我的文本文件如下所示 Person.txtC 如何从数据数组的结构中搜索特定结构并使用fread读取?,c,struct,fread,fseek,strstr,C,Struct,Fread,Fseek,Strstr,我的文本文件如下所示 Person.txt John { sex = "Male"; age = 23; }; Sara { sex = "Female"; age = 23; }; stephan { sex = "Male"; age = 25; }; 我想根据请求获取特定人员的数据。例如,我收到一个请求,要求获取Stephan的数据。我想,首先我需要阅读person.txt来搜索Stephan,然后获取他的信息。使用fread以正确的方式执行
John
{
sex = "Male";
age = 23;
};
Sara
{
sex = "Female";
age = 23;
};
stephan
{
sex = "Male";
age = 25;
};
我想根据请求获取特定人员的数据。例如,我收到一个请求,要求获取Stephan的数据。我想,首先我需要阅读person.txt来搜索Stephan,然后获取他的信息。使用fread以正确的方式执行此操作,我有点困惑。这是我的密码
struct personS
{
int age;
char sex[7];
} personS;
FILE *fp;
void check_person_data(const char *name, int *age, const char *sex)
{
PersonS *person;
if((fp=fopen("Person.txt", "r")) == NULL)
printf("File reading error\n");
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);
char buffer[size];
while(fread(buffer, size, 1, fp) != NULL)
{
if((strstr(buffer, name)) != NULL)
{
printf("Match found \n");
fread(&person, sizeof(struct personS), 1, fp);
*age = person->age;
*sex = person->sex;
}
else
printf("Match not found \n");
}
fclose(fp);
}
我做了两次fread,一次搜索字符串,另一次获取结构。这是正确的方法还是其他更好的方法 该示例似乎是一个文本文件,可以使用
fgets()
和sscanf()
读取。读取文件以查找匹配的名称不需要该结构,因此省略该结构。一旦读取了这些值,调用函数就可以将它们放入结构中
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int check_person_data(char *name, int *age, char *sex)
{
char buffer[50] = {0};
FILE *fp;
if ( ( fp = fopen ( "person.txt", "r")) == NULL) {
printf ( "file reading error\n");
return 2;
}
while ( fgets ( buffer, sizeof ( buffer), fp)) {
if ( buffer[0] == '\n') {
continue;//blank line
}
if ( buffer[strlen(buffer)-1] == '\n') {
buffer[strlen(buffer)-1] = '\0';//remove trailing newline
}
if ( strcmp ( buffer, name) == 0) {//found match
while ( fgets ( buffer, sizeof ( buffer), fp)) {
if ( strstr ( buffer, "sex")) {//does the line contain sex
if ( ( sscanf ( buffer, " %*[^\"\n]\"%6[^;\"\n]", sex)) != 1) {//scan and discard up to a ", scan " and scan up to six characters to next "
return 1;//bad record
}
}
if ( strstr ( buffer, "age")) {//does the line contain age
if ( ( sscanf ( buffer, " %*[^=\n]=%d", age)) != 1) {// scan and discard up to an =, scan = and scan an integer
return 1;//bad record
}
}
if ( strstr ( buffer, "};")) {//does the line contain };
break;//found end of record
}
}
return 0;//found the matching name
}
else {//did not match. read the remaining lines of record
while ( fgets ( buffer, sizeof ( buffer), fp)) {
if ( strstr ( buffer, "};")) {//does the line contain };
break;//found end of record
}
}
}
}
fclose ( fp);
return 1;//match not found
}
int main()
{
char name[30] = {0};
char sex[7] = {0};
int age = 0;
strcpy ( name, "stephan");
if ( ( check_person_data ( name, &age, sex)) == 0) {
printf ( "name %s age %d sex %s\n", name, age, sex);
}
else {
printf ( "%s not found\n", name);
}
return 0;
}
#包括
#包括
#包括
整数检查人员数据(字符*姓名,整数*年龄,字符*性别)
{
字符缓冲区[50]={0};
文件*fp;
if((fp=fopen(“person.txt”,“r”))==NULL){
printf(“文件读取错误\n”);
返回2;
}
while(fgets(buffer,sizeof(buffer),fp)){
如果(缓冲区[0]='\n'){
continue;//空行
}
if(buffer[strlen(buffer)-1]=='\n'){
buffer[strlen(buffer)-1]='\0';//删除尾部换行符
}
如果(strcmp(buffer,name)==0){//找到匹配项
while(fgets(buffer,sizeof(buffer),fp)){
if(strstr(buffer,“sex”){//行是否包含sex
如果((sscanf(缓冲区,“%*[^\”\n]\%6[^;\“\n]”,性别))!=1{//最多扫描并丢弃一个字符,最多扫描六个字符到下一个”
返回1;//错误记录
}
}
if(strstr(buffer,“age”){//行是否包含age
如果((sscanf(buffer,“%*[^=\n]=%d”,age))!=1{//扫描并丢弃一个=,扫描=并扫描一个整数
返回1;//错误记录
}
}
如果(strstrstr(buffer,“};”){//行是否包含};
break;//找到记录的结尾
}
}
返回0;//找到匹配的名称
}
else{//不匹配。请读取记录的其余行
while(fgets(buffer,sizeof(buffer),fp)){
如果(strstrstr(buffer,“};”){//行是否包含};
break;//找到记录的结尾
}
}
}
}
fclose(fp);
返回1//找不到匹配项
}
int main()
{
字符名[30]={0};
字符性别[7]={0};
int年龄=0;
strcpy(姓名,“stephan”);
如果((检查人员数据(姓名、年龄、性别))==0){
printf(“姓名%s年龄%d性别%s\n”,姓名、年龄、性别);
}
否则{
printf(“%s未找到\n”,名称);
}
返回0;
}
person现在是您文件中数据的副本,当您需要更改文件的数据时,您不能只更改副本,您需要
fwrite(..)
您更改的新数据我建议您使用fgets
读取长度未知的文本文件。它将数据读取到(包括)下一个换行符,因此,尽管您需要足够的空间来读取换行符,但在读取之前不需要知道需要多长时间。另一方面,fread
通常用于已知大小的二进制信息。我总是很高兴看到用于解析内容的中间缓冲区的sscanf。您的解决方案帮助了我,谢谢。现在我有另一个关于格式字符串的问题。例如,value=“0x1000”;我用fgets读这行,在处理格式时被卡住了。我尝试了使用sscanf(缓冲区“%*[^\”\n]\%D[^;\“\n]”,keyIntValue);也尝试了使用%x,但不起作用。请帮助?
fread(&person, sizeof(struct personS), 1, fp);