weixin_41646165 2018-07-16 02:53 采纳率: 0%
浏览 2691
已结题

C语言 为什么编译没问题运行时显示exe停止工作?

//main.c
#include
#include
#include "list.h"

int main()
{
struct Word *head;
head=creatLink();
listToFile(head);
return 0;
}

//list.h
#ifndef _LIST_H
#define _LIST_H

struct Word
{
char japanese[20];//日文
char kana[20];//假名
int accent;//声调
char property[10];//词性
char chinese[30];//中文
struct Sentense *head2;//句子结构体链表的头指针
struct Word *next;//下一个结点地址
};

struct Sentense
{
char sentense[50];//例句
struct Sentense *next;//下一个结点地址
};

FILE *inputOneWord_F(struct Word *p,FILE *fp);
struct Word *creatLink();
FILE *outputOneWord_F(struct Word *t,FILE *fp);
void listToFile(struct Word *head);

#endif // _LIST_H

//list.c
#include
#include
#include "list.h"
#define LEN sizeof(struct Word)
#define LEN2 sizeof(struct Sentense)

//将文件中一个Word结构体中的所有数据读入p所指向的链表结点,返回值为指针目前在文件中所在的位置
FILE *inputOneWord_F(struct Word *p,FILE *fp)
{
char a;
fscanf(fp,"%s%s%d%s%s",p->japanese,p->kana,&p->accent,p->property,p->chinese);
struct Sentense *g,*tail=NULL; //新建Sentense结构体单链表

 p->head2=NULL;
 while(1){
    fgetc(fp);//吸收换行符
    a=fgetc(fp);//通过是否为换行符来判断当前单词有无例句
    if(a=='\n'||feof(fp)) break;   //说明此单词无例句返回指针目前在文件中所在的位置,进行下一个单词的读取
    else{
      fseek(fp,-1l,SEEK_CUR);//光标向前一个字符
      fputc(a,fp);//将刚才从文件读取的字符按原样写入文件(位置不变)
      g=(struct Sentense*)malloc(LEN2);//
      fscanf(fp,"%s",g->sentense);
      if(p->head2==NULL)p->head2=g;//如果链表为空,新建立的结点就是链表的头结点
      else tail->next=g;
      tail=g;//tail永远指向链表的尾结点

     }
 }
 tail->next=NULL;
 return fp;

}

//在程序执行之初将上一次存储在文件中的全部数据读入新建Word结构体单链表,函数返回单链表头指针
struct Word creatLink()
{
struct Word *p,*head=NULL,*rear=NULL;
FILE *fp;
fp=fopen("WordList.txt","r");
if(fp==NULL)
{
printf("单词表为空!");
return head;
}
p=(struct Word
)malloc(LEN);
fp=inputOneWord_F(p,fp);//更改指针在文件中所在的位置
while(!feof(fp))
{
if(head==NULL)head=p;
else rear->next=p;
rear=p;
p=(struct Word*)malloc(LEN);
fp=inputOneWord_F(p,fp);
}
rear->next=p;
rear=p;
rear->next=NULL;
fclose(fp);
return head;
}

FILE *outputOneWord_F(struct Word *t,FILE *fp)
{

 fprintf(fp,"\n%s %s %d %s %s\n",t->japanese,t->kana,t->accent,t->property,t->chinese);
 struct Sentense *p;
 p=t->head2;  //p为指向Word结构体中Sentense结构体链表的头指针;
 while(p!=NULL)
 {
      fprintf(fp,"%s\n",p->sentense);
      p=p->next;
 }
 return fp;

}

//程序运行结束之际将链表中的数据全部写入文件
void listToFile(struct Word *head)
{
FILE *fp;
fp=fopen("WordList2.txt","w");
struct Word *p;
p=head;
while(p!=NULL)
{
fp=outputOneWord_F(p,fp);
p=p->next;
}
fclose(fp);
}

  • 写回答

15条回答 默认 最新

  • sunying1994 2018-07-16 03:00
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误