c - 如何将文件中的数据分配给数据结构

标签 c file data-structures

我是编程初学者,我正在获得一些帮助。我有一个文件,必须将该文件的数据(名称、地址、序列号、功能)分配给一个数据结构。我怎样才能做到这一点?

#include<stdio.h>
 
typedef struct
int age;
char addres[50];
long serial_num;
char function[25];
} pers;
 
 main(){
int i, n, a1;
char num_tot[21]= "Number of person: ";
FILE *f1;
//pers p[n];
f1=fopen("bd.txt", "r");
while(!feof(f1)){
    if(fread(&a1, sizeof(f1),1,f1)==num_tot[21]) //bad idea
{

}
}

//文件

Number of person: 3
 
1.Name: Andrei Ungureanu 
Age: 27
Addres: Chisinau, str. Vasile Alexandri 94a
Serial number: 245578
Function: secretary
 
2.Name: Boris Macari 
Age: 24
Addres: Chisinau, str. 27 Martie 1918 56
Serial number: 787791
Function: general auditor
 
3.Name: Corina Lupu 
Age: 43
Adresa: Chisiau, str. Liviu Deleanu 9 
Serial number: 983345 
Function: general manager

最佳答案

我只是分享我为您的问题编写的代码。
请记住,我的代码很大程度上取决于您文件的一般结构!
希望这对您有帮助

#include <stdio.h>
#include <string.h>

using namespace std;

typedef struct
{
    int age;
    char addres[50];
    long serial_num;
    char function[25];
} pers;

int main()
{
    FILE *f1;
    f1 = fopen("bd.txt", "r+");

    int n = 0;

    char line[100];
    char ch;

    int i = 0;

    while((ch = fgetc(f1)) && ch!='\n')
    {
        line[i++] = ch;
    }

    for(int k = 18 ; k < i; k++)
    {
        n = n*10 +(line[k]-'0'); 
    }

    pers p[n];

    memset(line , '\0', i);
    i = 0;


    for(int m = 0; m < n; m++)
    {
        while((ch = fgetc(f1)) && ch!='\n')     //blank line 
        {
            continue;
        }

        while((ch = fgetc(f1)) && ch!='\n')     //line of name(we don't need this data)
        {
            continue;
        }

        /////////////

        while((ch = fgetc(f1)) && ch!='\n')     //line of age
        {
            line[i++] = ch;
        }
        int j = 5;
        p[m].age = 0;
        while(line[j] != ' ' && line[j] != '\0')
            p[m].age = p[m].age*10 + (line[j++]-'0');

        memset(line , '\0', i);
        i = 0;

        /////////////

        while((ch = fgetc(f1)) && ch!='\n')     //line of address
        {
            line[i++] = ch;
        }
        j = 8;
        while(j != i)
            p[m].addres[j-8] = line[j++];

        memset(line , '\0', i);
        i = 0;

        //////////////

        while((ch = fgetc(f1)) && ch!='\n')     //line of serial_number
        {
            line[i++] = ch;
        }
        j = 15;
        p[m].serial_num = 0;
        while(line[j] != ' ' && line[j] != '\0')
            p[m].serial_num = p[m].serial_num*10 + (line[j++]-'0');

        memset(line , '\0', i);
        i = 0;

        //////////////

        while((ch = fgetc(f1)) && ch!='\n' && ch != EOF)        //Line of function
        {
            line[i++] = ch;
        }
        j = 10;
        while(j!=i)
            p[m].function[j-10] = line[j++];

        memset(line , '\0', i);
        i = 0;
    }


//  for (int m = 0; m < n; m++)
//  {
//      printf ("%s *** %d *** %s *** %ld\n", p[m].addres , p[m].age , p[m].function , p[m].serial_num);
//  }

    return 0;
}

关于c - 如何将文件中的数据分配给数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49829260/

相关文章:

c - 将项目从 64 位移植到 32 位时 : float changed to long double gives error for %f

python 在 readline() 之后调用 read() 没有获取整个剩余文件

c - FreeBSD 数据结构与通信

java - 数据结构与算法实现-字典

algorithm - 允许线性布局的快速算法/数据结构?

我们可以在 getopt 中添加更多选项吗?

c - 将 __VA_ARGS 用于 printf

c - 堆上的字符串指针与被调用函数的堆栈

c - 如何在 C 编程中锁定文件并避免在写入时读取文件

将文本文件的数据转换为变量