问题描述
- 关于文件指针的一个错误
-
/************************************************************************************/ /* 著作権所有者 : / /* 文件名 :电子地图管理系统 */ /* 内容 : 对一个二进制地图文件进行处理,排序,检索,更新 */ /* 日期:2015-7-31 */ /* */ /* */ /*************************************************************************************/ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<windows.h> #include<malloc.h> #include<conio.h> void UI1(); //基本服务界面 void UI2();//会持续出现的服务选择界面 void server();//服务信息,提示用户输入数字 int rf(); void sort(); void search(); void RD(FILE *p); void update(); int nsizecount=2; int n = rf(); struct RoadRecord { short ussize; long ullinkId; int usdispclass; int usbrunch; int usroadnameflag; char roadname[20]; }; struct Map_t { struct RoadRecord road[700000] ; }*s,*ss; struct Map_t road_t; int M_t1 = 0; int M_t2 = 0; #define MCGETLONG(data) ( ( ((unsigned long)MCGETSHORT((data)))<<16 )|( ((unsigned long)MCGETSHORT((char *)(data)+2))&0x0000ffff)) #define MCGET3BN(data) ( (((unsigned long )MCGETCHAR((data)))<<16)|(((unsigned long)MCGETSHORT((char *)(data+1))&0x0000ffff)) #define MCGETCHAR(data) (*((char *)(data))) #define MCGETSHORT(data)((unsigned short)((((unsigned short)(*((char *)(data))))<<8)|(((unsigned short)(*((char *)(data)+1)))&0x00ff))) #define Length 700000 void UI1(){ printf("/********************---Welcome!---*******************"); printf(" "); printf("/*** 电子地图导航系统 ***/"); printf(" "); printf("/*** team9 陈永胜 李想 孙林桐 ***/"); printf(" "); printf("/*** 2015.7.31 ***/"); printf(" "); printf("/****************************************************/"); } void UI2(){ printf(" "); printf("请选择服务的种类:"); printf(" "); printf(" 1.读取文件(“./data/GTBL.dat”)"); printf(" "); printf(" 2.排序(快速排序)并输出排序结果(“./data/SortGTBL.dat”)"); printf(" "); printf(" 3.检索"); printf(" "); printf(" 4.更新"); printf(" "); printf(" 0.退出"); printf(" "); printf(" 请选择"); } void server(){ int n; FILE *p; scanf("%d",&n); switch(n){ case 1: rf(); //调用读取文件函数 printf("文件读取成功! "); printf("Success! "); s=(struct Map_t*)malloc(sizeof(struct Map_t)*(nsizecount-2)); ss=(struct Map_t*)malloc(sizeof(struct Map_t)*(nsizecount-2)); p=fopen("d:dataa.txt","r"); M_t1++; RD(p); UI2(); server(); break; case 2: if(M_t1 == 0){ printf("请先读取文件,输入1 "); UI2(); server(); } else if(M_t1 == 1){ M_t2++; sort(); } //调用排序文件函数 break; case 3: if(M_t1 ==0 || M_t2 ==0) { printf("先读取并排序文件,才可以检索 "); UI2(); server(); } else { search(); } //调用搜索文件函数 break; case 4: if(M_t1 ==0 || M_t2 ==0) { printf("先读取并排序文件,才可以更新 "); UI2(); server(); } else { update(); }//调用更新文件函数 break; case 0: exit(0); //调用退出函数 break; default : printf("输入错误请重新输入:"); server(); break; } } int rf(){ int m; char aclinkId[4]; char acroadnamesize[2]; char acRecordinfo[4]; unsigned short ustotalsize; unsigned long ullinkId; unsigned short usroadnamesize; char actotalsize[2]; FILE *p; FILE *pl; p=fopen("f:dataGTBL.dat","rb+"); pl=fopen("f:dataa.txt","w"); if(p==NULL) { printf("文件不存在! "); } else { while(fread(actotalsize,sizeof(actotalsize),1,p)==1) { fread(aclinkId,sizeof(aclinkId),1,p); fread(acroadnamesize,sizeof(acroadnamesize),1,p); fread(acRecordinfo ,sizeof(acRecordinfo),1,p); ustotalsize=MCGETSHORT(actotalsize); ullinkId=MCGETLONG(aclinkId); usroadnamesize=MCGETSHORT(acroadnamesize); road_t.road[nsizecount].ussize=ustotalsize; road_t.road[nsizecount].ullinkId=ullinkId; m=(int)acRecordinfo[3]&255; road_t.road[nsizecount].usdispclass=m&15; road_t.road[nsizecount].usbrunch=(m&112)/16; road_t.road[nsizecount].usroadnameflag=(m&128)/128; fread(road_t.road[nsizecount].roadname,sizeof(char),ustotalsize-12,p); fprintf(pl,"# "); fprintf(pl,"LinkID="); fprintf(pl,"%d ",road_t.road[nsizecount].ullinkId); fprintf(pl,"flag="); fprintf(pl,"%d ",road_t.road[nsizecount].usroadnameflag); fprintf(pl,"brunch"); fprintf(pl,"%d ",road_t.road[nsizecount].usbrunch); fprintf(pl,"dispclass="); fprintf(pl,"%d ",road_t.road[nsizecount].usdispclass); fprintf(pl,"Roadname=1="); fprintf(pl,"%s ",road_t.road[nsizecount].roadname+4); fprintf(pl,"#"); fprintf(pl," "); nsizecount++; } fclose(p); fclose(pl); // printf("%d",nsizecount); } return nsizecount; } void update(){ } void search(){ printf("请选择检索的方式: "); printf(" 1.指定linkID检索 "); printf(" 2.指定交叉Link列表示ClassF番号 检索 "); printf(" 3.指定查找岔路数 检索 "); printf(" 4.指定道路名称 检索 "); printf(" 0.返回 "); int n; int linkID; int fh; int cl; int count1=0; int count2=0; char name; scanf("%d",&n); int count = 0; switch(n) { case 1: int low,high; int key; int mid; low = 0; high = nsizecount - 1; printf(" 请输入LinkID:"); scanf("%d",key); while(low<high) { count++; mid = (low + high)/2; if(key<road_t.road[mid].ullinkId) high = mid - 1; else if(key > road_t.road[mid].ullinkId) low = mid + 1; else if(key==road_t.road[mid].ullinkId) { printf(" LinkID=%s ",road_t.road[mid].ullinkId); printf("flag=%s ",road_t.road[mid].usroadnameflag); printf("brunch=%s",road_t.road[mid].usbrunch); printf("displace=%s",road_t.road[mid].usdispclass); printf("Roadname=%s",road_t.road[mid].roadname+4); break; } } int i; for(i=0;i<=1000;i++) { printf("%d ",road_t.road[i].ullinkId); } break; case 2: printf(" 请输入交叉link列表示class番号:"); scanf("%s",&fh); for (i=0;i<n-2;i++) { if (fh==road_t.road[i].usdispclass) { printf("%d",road_t.road[i].usdispclass); count1++; } if (count1==0) { printf(" 没有匹配结果,请重新选择"); } } break; case 3: printf(" 请输入岔路数:"); scanf("%d",&cl); for (i=0;i<n-2;i++){ if (cl==road_t.road[i].usbrunch) { printf("%d",road_t.road[i].usbrunch); count2++; } } if (count2==0) { printf(" 没有匹配结果,请重新选择"); break; } break; case 4: printf("请输入道路名称:"); scanf("%s",name); break; case 0: system("cls"); UI2(); server(); break; default: printf("输入错误,请重新输入 "); search(); } } void sort(){ } void RD(FILE *p) { if( M_t1 != 0) { int i=0; printf(" 文件信息----写入内存中... ... "); for(i=0;i<n-2;i++) { fscanf(p,"%s",road_t.road[i].ullinkId); fscanf(p,"%s",road_t.road[i].usroadnameflag); fscanf(p,"%s",road_t.road[i].usbrunch); fscanf(p,"%s",road_t.road[i].usdispclass); fscanf(p,"%s",road_t.road[i].roadname); } if(road_t.road[5].ullinkId != NULL) printf(" 文件信息----写入内存成功 "); UI1(); server(); } else { UI1(); server(); } } int main() { UI1(); UI2(); server(); return 0; }
解决方案
解决方案二:
http://download.csdn.net/detail/monster88ra/4928285
解决方案三:
什么错误呢?没有看到,也不喜欢猜。
解决方案四:
请给出问题所在,乐意解答……
时间: 2024-10-30 11:51:20