- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的文本文件是这样的,
Person.txt
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);
}
我做了 2 次 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;
}
关于c - 如何从数据数组的结构中搜索特定结构并使用 fread 读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30462559/
我有以下代码: int main() { char* pedal[20]; char* pedal2[20]; for (int i = 0; i < 20; i++)
我想用 in.wav 文件中的数据填充 hdr (结构)变量,并且我想复制 in 的前 64 个字节。 wav 文件转换为另一个文件 (out.wav)。 但是!当第二次使用fread()时,它开始从
我有一个由 1and1 托管的网站 - 他们没用!由于某种原因,他们提供的备份脚本不再有效,他们无法为我提供答案!所以我想我会自己写,这是我的代码: if (file_exists('backup
我正在尝试从文件中读取并将其复制到另一个文件。我正在网上查看一些代码,我似乎注意到有些人以这种方式声明 fread: fread (buffer, 1, 1000, src) 一些这样 fread (
当我是“男人的恐惧”时,我得到了: RETURN VALUE fread() and fwrite() return the number of items successfully read or
从文件中读取整数值时,覆盖率检查给出以下错误 调用函数“fread”会污染参数“readval” //coverity note: Calling function "fread" taints ar
为了更清楚地说明这一点,我将放置代码示例: $file = fopen('filename.ext', 'rb'); // Assume $pos has been declared // metho
尝试转换此 matlab 代码: fid = fopen([fpath, '/file.bin'],'rb'); content = fread(fid, 11,'single'); 我当前的尝试如下
假设我有: FILE* fp = fopen("myfile.bin", "r"); char something[30]; fread(something,sizeof(char)*30,1,fp)
fwrite 一个整数取决于字节序,但是有没有一种方法可以将一个整数 0x00000004 写入一个文件,这样无论它运行在什么机器上,它都可以始终被读取为 0x00000004。 一个想法是始终按照特
所以我尝试将此类 Matlab 代码转换为 C++: ss = 'file.mask' fp = fopen(ss, 'rb'); sx = fread(fp, 1, 'int32') sy = f
使用 C,可以使用函数 fread 来读取以 null 结尾的字符串吗? 我必须读取一个以 ip 开头的文件,该文件是 4 个无符号字符,后跟一个描述空终止字符串数的整数。之后,我需要读取字符串,直到
> fread('col1,col2\n') Empty data.table (0 rows) of 2 cols: col1,col2 > fread('col1,col2\n5,4') c
我正在尝试使用 data.table 将文件读入 R/fread .一些字段有前导零,我只想将数据作为字符读取并手动修复它们。但是我不知道如何将其传达给 fread .我正在尝试这个,它像往常一样分配
fread来自 data.table包一般可以在读取文件时自动确定列分隔符( sep )。 例如,这里fread自动检测 |作为列分隔符: library(data.table) fread(past
使用 fread,如何读取包含行名和列名的 CSV 文件。 我尝试了以下操作,但它没有正确读取行和列名称。 csv 文件看起来像(其中 C1、C2、C3 是列名,r1、r2、r3 是行名) input
我遇到了这样的文件: COL1 COL2 COL3 weqw asrg qerhqetjw weweg ethweth
我正在尝试使用 fread 读取表格。 txt 文件具有如下所示的文本: "No","Comment","Type" "0","he said:"wonderful|"","A" "1","Pr/ "
我正在尝试使用从 Apple 移动性报告生成的 csv,可以找到 here . 现在一切正常,我能够按预期获得 .csv,它看起来像这样的文字: csvtxt <- "geo_type,region,
我在 data.table (1.8.8, R 3.0.1) 中使用 fread 试图读取非常大的文件。 有问题的文件有 313 行和约 660 万列数字数据行,文件大小约为 12GB。这是具有 51
我是一名优秀的程序员,十分优秀!