2022年C语言文件读取 .pdf
《2022年C语言文件读取 .pdf》由会员分享,可在线阅读,更多相关《2022年C语言文件读取 .pdf(10页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、D:TDDOWNLOADmyclass2class.cpp2011年 12月 7日 16:03/*class.cpp1、从文本文件中导入班级学生信息:学号、姓名、性别、籍贯2、将学号重复的删除3、显示导入的学生信息4、按学号、姓名、性别、籍贯相等和不相等查找5、多次查找6、查找结果写入文件7、 VC+6.0编译通过*文件尾部有class.txt 班级信息*/#include #include #include #include #include #include #define CLRSCRsystem(cls)#define PRINT_TITLE n序号 t学号 tt姓名 t性别 t籍贯
2、n#define PRINT_FORMAT %dt%st%st%st%sn,i,p-stu.num,p-stu.name,p-stu.sex,p-stu.hometown#define WRITE_FORMAT %st%st%st%sn,p-stu.num,p-stu.name,p-stu.sex,p-stu.hometown#define READ_FORMAT %s %s %s %s,&p-stu.num,&p-stu.name,&p-stu.sex,&p-stu.hometown/定义学生结构体structstudentcharnum 9;charname 7;/三个汉字长度为6个字节,
3、如果不多定义一个字节来存放字符串结束符0,当输出name 时会把 sex 和 hometown也输出charsex 3;charhometown 7;/定义单链表结点typedefstructlistnodestructstudentstu ;structlistnode* next; node ;intMySelect( node*head , node* temp )/返回查找到符合条件的项目数intequal, N;-1-名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,
4、共 10 页 - - - - - - - - - D:TDDOWNLOADmyclass2class.cpp2011年 12月 7日 16:03charCHAR 10 ;printf( -nn);printf( 查找模式:1 是相等查找,0 是不相等查找n);printf( 列名编号:1 是学号,2 是姓名,3 是性别,4 是籍贯n);printf( n-n);doprintf( n选择查找模式: );/用 scanf时输入字母 会 错误改用 getche,不 用 scanfequal=getche();if( equal!= 1)&( equal!= 0)printf( nt输入 错误 n)
5、;while( equal!= 1)&( equal!= 0);equal=equal- 48 ;doprintf( n输入列名编号: );N=getche();if( N!= 1&N!= 2&N!= 3&N!= 4)printf( nt输入 错误 n);while( N!= 1&N!= 2&N!= 3&N!= 4);switch( N)case1:N=0; break;case2:N=9; break;case3:N=16 ; break;case4:N=19 ; break;printf( n输入 关键 字 : );scanf( %s , CHAR);inti =0, flag;node*
6、 p,* tp =temp ;p=head - next;while( NULL != p)flag=strcmp( char*) p+N, CHAR);if( abs ( flag)+ equal)= 1) /当 equal为 1时是相等查找, 为 0时是不相等查找node* end ;end=( node*) malloc( sizeof( node );tp - next=end ;tp =end ;-2-名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 10 页 -
7、 - - - - - - - - D:TDDOWNLOADmyclass2class.cpp2011年 12月 7日 16:03tp - next=NULL;tp - stu =p- stu ;if( i = 0)printf( PRINT_TITLE);i +;printf( PRINT_FORMAT );p=p- next;if( i =0)printf( nt没 有查找到符合条件的信息n);returni ;voidInsertSort( node* head ) / 学号从小 到 大排 序node* p,* q,* r ,* u;p=head - next;head - next=NU
8、LL;while( p!= NULL )r =head ;q=head - next;while( q!= NULL & strcmp( q- stu . num, p- stu . num) next;u=p- next;p- next=r - next;/p-stu.num比 q-stu.num小 ,放到q-stu.num前面r - next=p;p=u; /下 一 上 节点 与基 点 进行比较voidCheckNum ( node* head )/* 删除学号重复的条目*/node* release,* p;p=head - next;intm=0;while( NULL != p- n
9、ext)-3-名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 10 页 - - - - - - - - - D:TDDOWNLOADmyclass2class.cpp2011年 12月 7日 16:03node* p2 =p;while( NULL!= p2 - next)if( 0=strcmp( p- stu . num, p2 - next- stu . num)m+;if( m=1)printf( tt以下 条目 因 学号 与前面的信息冲突而没有导入n);prin
10、tf( %dt%st%st%st%sn, m, p2 - next- stu . num, p2 - next- stu .namep2 - next- stu . sex , p2 - next- stu . hometown);release=p2 - next;p2 - next=p2 - next- next;free( release);p2 =p2 - next;p=p- next;voidFscanfFromFile( node* head )charfile 15 ;printf( 输入 待 导入的文件名: );scanf( %s , file);FILE* fp ;if( f
11、p =fopen( file, r)=NULL )printf( nt文件 打开失败,按 任意键退出 n);getch();exit( 0);node* p=head ;while(! feof( fp )node* end ;end= ( node*) malloc( sizeof( node );p- next=end ;p=end ;p- next=NULL ;fscanf( fp , READ_FORMAT);-4-名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共
12、10 页 - - - - - - - - - D:TDDOWNLOADmyclass2class.cpp2011年 12月 7日 16:03printf( nt信息导入成功 n);fclose( fp );voidCheckNum ( node* head );voidInsertSort( node* head );CheckNum ( head );InsertSort( head );voidShowList( node* head ) /带头 结点的链表node* p=head - next;inti =0;printf( PRINT_TITLE);while( NULL != p)
13、/*NULL!=p 不是 NULL!=p-nexti +;printf( PRINT_FORMAT );p=p- next;voidFprintfToFile( node* head )charfile 15 ;printf( n输入 要 写入的文件名: );scanf( %s , file);FILE* fp ;if( fp =fopen( file, w+ )=NULL)printf( nt文件 创建失败n);exit( 0);node* p=head - next;while( NULL != p)if( p- next=NULL ) /不 要最后一 行 的回 车 , 这样 的 话 导入
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年C语言文件读取 2022 语言 文件 读取
限制150内