簡體   English   中英

如何從數據數組的結構中搜索特定結構並使用fread進行讀取?

[英]How to search for specific structure from Structures of data array and read using fread?

我的文本文件看起來像這樣,

Person.txt

John 
{
   sex = "Male";
   age = 23;
};

Sara 
{
   sex = "Female";
   age = 23;
};

stephan 
{
   sex = "Male";
   age = 25;
};

我想根據請求獲取特定人的數據。 對於實例,我收到了提取斯蒂芬數據的請求。 我想,首先我需要閱讀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);
}

我做了兩次,一次是搜索字符串,另一次是獲取結構。 這是正確的方法還是其他更好的方法?

該示例似乎是一個文本文件,可以用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;
}
fread(&person, sizeof(struct personS), 1, fp);  

現在,它是您文件中數據的副本,當您需要更改文件數據時,您不能只更改副本,您需要fwrite(..)您已更改的新數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM