学员小问题,在线等,急

Super_Xiaoze 2020-01-01 11:15:55
我想写个程序,利用fgets读取文档中每一行录入数组中,最后再录入结构体中,可测试时就是输出不出来,怎么办,求大佬们帮帮我。
文档中字符串是以\t 隔开的。
#include <stdio.h>
#include <stdlib.h>
struct YC
{
char name[30];
char Cp_cn[80];
char Cp[80];
char N[500];
};
int main()
{
struct YC A[10000];
FILE *fp =fopen("Herb_Compound.txt","r");
if((fp=fopen("Herb_Compound.txt","r"))==NULL)
{
printf("Can not open file\n");
}
char a[1000];
int i=0,j=0,e=0,f=0,p=0,q=0;
while(!feof(fp))
{
fgets(a,500,fp);
while(a[i]!='\t')
{
A[j].name[q]=a[i];
i++;
q++;
}
while(a[i]='\t')
{
i++;
}
while(a[i]!='\t')
{
A[j].Cp_cn[e]=a[i];
e++;
i++;
}
while(a[i]='\t')
{
i++;
}
while(a[i]!='\t')
{
A[j].Cp[f]=a[i];
f++;
i++;
}
while(a[i]='\t')
{
i++;
}
while(a[i]!='\n')
{
A[j].N[p]=a[i];
p++;
i++;
}
j++;
i=0;
}
for(int i=0;i<j;i++)
{
for(int w=0;i<q;i++)
{
printf("%c",A[j].name[i]);
}
printf(" ");
for(int w=0;i<e;i++)
{
printf("%c",A[j].Cp_cn[i]);
}
printf(" ");
for(int w=0;i<f;i++)
{
printf("%c",A[j].Cp[i]);
}
printf(" ");
for(int w=0;i<p;i++)
{
printf("%c",A[j].N[i]);
}
printf("\n");
}
return 0;
}

...全文
15 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
源代码大师 2021-05-06
  • 打赏
  • 举报
回复
希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10581430.html 希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10768339.html
寻开心 2020-01-01
  • 打赏
  • 举报
回复
1 fopen两次是不对的。 FILE * fp; 就可以了
2 你先试试fgets后puts看看,是否从文件当中正确的获得了字符串
3 把文本读入到a数组后,你用sscanf函数读取就不用那么多while语句判断
sscanf 和 scanf函数类似
sscanf(a, "%s%s%s%s", name, id, cp, ....

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧