爱问知识人 爱问教育 医院库

c语言高手请进!50分题!

首页

c语言高手请进!50分题!

如何从一个文本文件中读入信息,并建立链表啊?
  比如:从文件INA。TXT读入学生信息,建立链表A,从文件INB。TXT读入学生信息,建立链表B,其中每个链表中结点包括学号,姓名,成绩将两个链表合并到A,按学号升序排列,最后将链表A中的学生信息输出到文件OUT。TXT中。 

该怎么办啊?

提交回答
好评回答
  • 2005-09-05 09:40:07
      呵呵,好久没编了,都忘光了,重新复习一下。
    为简单计,只作出 【链表←→文件】 双向的读写,而链表的排序简单、基本,在此省略。
    【程序步骤】:
    	ina。txt↘
    	          链表→不排序,直接输出到屏幕→输出到Out。txt
    	inb。
      txt↗ 【最为关键的地方】: "%20d%20s%20d" 链表以如上格式保存、读写到文件。很好理解,以“学号20位,姓名20、成绩20”的格式进行读写。 【调试环境】: Visual C++ 6。0 英文企业版 并在“Borland C++ 5。
      5 编译器 For DOS”测试通过 Win XP SP2中文版 【附件】: ina。txt ↘ inb。txt 一→ 打包成附件。RaR Test。cpp ↗ 【源程序】: #include #include //#include #define NULL 0 #define FORMAT "%20d%20s%20d" #define LEN sizeof(student) struct student { int number; /*学号*/ char name[10]; /*姓名*/ int score; /*成绩*/ struct student *pNext; }; void main() { FILE *fposition = NULL; student *pOne = NULL, *pTwo = NULL, *pHead = NULL; _flushall(); // 以下代码打开ina。
      txt,并读入链表 if ((fposition = fopen ("ina。txt", "r")) == NULL) { printf("\n打开文件失败\n"); exit(0); } pOne = (student *) malloc(LEN); pTwo = pOne; pHead = pOne; pHead->pNext = NULL; while (!feof(fposition)) { if (!fscanf(fposition, FORMAT, &pOne->number, &pOne->name, &pOne->score)) printf("\n读入文件出错,可能文件为空,或文件不存在!\n"); pTwo = pOne; pOne = (student *) malloc(LEN); pTwo->pNext = pOne; } // 以下代码打开inb。
      txt,追加到链表 if ((fposition = fopen ("inb。txt", "r")) == NULL) { printf("\n打开文件失败\n"); exit(0); } while (!feof(fposition)) { if (!fscanf(fposition, FORMAT, &pOne->number, &pOne->name, &pOne->score)) printf("\n读入文件出错,可能文件为空,或文件不存在!\n"); pTwo = pOne; pOne = (student *) malloc(LEN); pTwo->pNext = pOne; } pTwo->pNext = NULL; pOne = pHead; // 以下代码输出链表,并保存到Out。
      txt if(pOne == NULL) { printf("\n数据为空!\n"); } else { printf("\t\t学号\t\t姓名\t\t\t成绩\n"); if ((fposition = fopen ("out。
      txt", "w")) == NULL) { printf("\n打开文件失败\!n"); exit(0); } while (pOne != NULL) { printf(FORMAT, pOne->number, pOne->name, pOne->score); printf("\n"); if (! fprintf(fposition, FORMAT, pOne->number, pOne->name, pOne->score)) { printf("\n读入文件出错!\n"); } pOne = pOne->pNext; } fclose(fposition); free(pOne); /*释放*/ pOne = NULL; free(pTwo); /*释放*/ pTwo = NULL; free(pHead); /*释放*/ pHead = NULL; } }。
      

    b***

    2005-09-05 09:40:07

  • 软件 相关知识

  • 电脑网络技术
  • 电脑网络

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):