使⽤c语⾔如何将txt⽂档内容读到结构体中程序源码
#include<stdio.h>
#include <string.h>
//可以退出的头⽂件
#include <stdlib.h>
//结构体的长度
#define DATALEN 15
//函数声明
//定义结构数组
struct wordUnit{
int id;//id
c语言struct头文件
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("","r"))==NULL){
printf("Can not open file\n");
return0;
}
while(!feof(data)){
//原txt⽂档的数据之间是以空格隔开的
fscanf(data,"%d %s %s %s %s\n",&words[i].id,&words[i].word,&words[i].depId,&words[i].pos,&words[i].depRel);
i++;
}
fclose(data);
for(int j=0;j<i;j++){
printf("%d %s %s %s %s\n",words[j].id,words[j].word,words[j].depId,words[j].pos,words[j].depRel);
}
return0;
}
测试结果
读取的txt⽂档内容
1 我们 none r none
2 即将 none d none
3 以 6 p pob
4 昂扬
5 a rad
5 的 none u none
6 ⽃志 4 n att
7 迎来 1,2,3,10 v hed,sbv,adv,wp
8 新 9 a rad
9 的 none u none
10 ⼀年 8 i vob

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。