C语言怎样将.txt文件中的数据写入到结构体中去

C语言怎样将.txt文件中的数据写入到结构体中去,第1张

txt文件中的数据写入到结构体中去的源代码如下:

#include<stdio.h>

#include <string.h>

//可以退出的头文件

#include <stdlib.h>

//结构体的长度

#define DATALEN 15

//函数声明

//定义结构数组

struct wordUnit{

int id//id

char word[10]//词语

char depId[10]//依存词语的id

char pos[10]//词性

char depRel[10]//依存目标的关系

}

int main(){

FILE *data//要读取的文件指针

int i=0//结构题数组移动

struct wordUnit words[DATALEN]

if((data=fopen("data3.txt","r"))==NULL){

printf("Can not open file\n")

return 0

}

while(!feof(data)){

//原txt文档的数据之间是以空格隔开的

}

fclose(data)

for(int j=0j<ij++){

}

return 0

}

扩展资料

1、使用关键字struct,它表示接下来是一个结构体。

2、后面是一个可选的标志(book),它是用来引用该结构体的快速标记。

一般有两种方法.

struct A

{

    int a

    float f

    char s[10]

}m

为例:

一种是写文本文件

以"w"打开

fprintf(fp, "%d %f %s\n", m.a,m.f, m.s)

另一种是写二进制文件.

以"wb"打开

fwrite(&m, sizeof(m), 1, fp)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12074349.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存