实现一个将结构类数据写入二进制文件中,代码test6_12.c
1 //This is c program code! 2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= 3 * 文档信息: *** :~/WORKM/stutyCode/cCode/recipesProblemSolution/chapter06/test6_12.c 4 * 版权声明: *** :(魎魍魅魑)MIT 5 * 联络信箱: *** :guochaoxxl@163.com 6 * 创建时间: *** :2020年11月21日的下午04:25 7 * 文档用途: *** :数据结构与算法分析-c语言描述 8 * 作者信息: *** :guochaoxxl(http://cnblogs.com/guochaoxxl) 9 * 修订时间: *** *2020年第46周 11月21日 星期六 下午05:03 (326天) 10 * 文件描述: *** :自行添加 11 * *+=+=+=+=* *** *+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/ 12 #include <stdio.h> 13 14 typedef struct _bio{ 15 char name[15]; 16 int rollNo; 17 int age; 18 float weight; 19 } Bio; 20 21 void closeState(int clo, FILE *fPtr){ 22 clo = fclose(fPtr); 23 if(clo == -1){ 24 puts("File-closing failed!"); 25 } 26 if(clo == 0){ 27 puts("File is closed successfully."); 28 } 29 30 return; 31 } 32 33 int main(int argc, char **argv) 34 { 35 char flag = 'y'; 36 FILE *fPtr = fopen("agentsb.dat", "wb"); 37 if(fPtr != NULL){ 38 printf("File agentsb.dat is opened successfully. "); 39 Bio bio; 40 while(flag == 'y'){ 41 printf("Enter name, rooNo, age and weight of agent: "); 42 scanf("%s %d %d %f", bio.name, &bio.rollNo, &bio.age, &bio.weight); 43 fwrite(&bio, sizeof(bio), 1, fPtr); 44 int tmp = getchar(); 45 //fflush(stdin); 46 printf("Any more records(y/n): "); 47 scanf("%c", &flag); 48 } 49 int clo = fclose(fPtr); 50 closeState(clo, fPtr); 51 }else{ 52 puts("File-open failed!"); 53 } 54 55 return 0; 56 }
编译:
gcc -g test6_12.c -o test6_12
运行错误如下:
File agentsb.dat is opened successfully. Enter name, rooNo, age and weight of agent: zhangsan 1 21 74.5 Any more records(y/n): y Enter name, rooNo, age and weight of agent: wangsi 2 22 75.5 Any more records(y/n): y Enter name, rooNo, age and weight of agent: liwu 3 33 76.5 Any more records(y/n): y Enter name, rooNo, age and weight of agent: zhaoliu 4 44 77.5 Any more records(y/n): y Enter name, rooNo, age and weight of agent: zhouqi 5 55 78.5 Any more records(y/n): n free(): double free detected in tcache 2
拿出调试的40米大刀长,GDB上场; 输入:gdb test6_12
1 GNU gdb (GDB) 10.1 2 Copyright (C) 2020 Free Software Foundation, Inc. 3 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 4 This is free software: you are free to change and redistribute it. 5 There is NO WARRANTY, to the extent permitted by law. 6 Type "show copying" and "show warranty" for details. 7 This GDB was configured as "x86_64-pc-linux-gnu". 8 Type "show configuration" for configuration details. 9 For bug reporting instructions, please see: 10 <https://www.gnu.org/software/gdb/bugs/>. 11 Find the GDB manual and other documentation resources online at: 12 <http://www.gnu.org/software/gdb/documentation/>. 13 14 For help, type "help". 15 Type "apropos word" to search for commands related to "word"... 16 Reading symbols from test6_12... 17 (gdb)
基本都是关于gdb的信息,主要有版本,版权即说明,
1、第14行开始,可以键入help获取帮助
2、第15行开始,可以apropos word查找关键字信息
3、第16行开始,从文件test6_12读取调试符号
help信息为:
1 help 2 List of classes of commands: 3 4 aliases -- User-defined aliases of other commands. 5 breakpoints -- Making program stop at certain points. 6 data -- Examining data. 7 files -- Specifying and examining files. 8 internals -- Maintenance commands. 9 obscure -- Obscure features. 10 running -- Running the program. 11 stack -- Examining the stack. 12 status -- Status inquiries. 13 support -- Support facilities. 14 text-user-interface -- TUI is the GDB text based interface. 15 tracepoints -- Tracing of program execution without stopping the program. 16 user-defined -- User-defined commands. 17 18 Type "help" followed by a class name for a list of commands in that class. 19 Type "help all" for the list of all commands. 20 Type "help" followed by command name for full documentation. 21 Type "apropos word" to search for commands related to "word". 22 Type "apropos -v word" for full documentation of commands related to "word". 23 Command name abbreviations are allowed if unambiguous.
查看源码:l
(gdb) l 20 21 void closeState(int clo, FILE *fPtr){ 22 clo = fclose(fPtr); 23 if(clo == -1){ 24 puts("File-closing failed!"); 25 } 26 if(clo == 0){ 27 puts("File is closed successfully."); 28 } 29
设置断点:b closeState,通过函数名
(gdb) b closeState Breakpoint 1 at 0x11b8: file test6_12.c, line 22.
设置断点:b 36,通过行号
(gdb) l 30 return; 31 } 32 33 int main(int argc, char **argv) 34 { 35 char flag = 'y'; 36 FILE *fPtr = fopen("agentsb.dat", "wb"); 37 if(fPtr != NULL){ 38 printf("File agentsb.dat is opened successfully. "); 39 Bio bio; (gdb) b 36 Breakpoint 2 at 0x1211: file test6_12.c, line 36.
设置断点:b 49
(gdb) l 40 while(flag == 'y'){ 41 printf("Enter name, rooNo, age and weight of agent: "); 42 scanf("%s %d %d %f", bio.name, &bio.rollNo, &bio.age, &bio.weight); 43 fwrite(&bio, sizeof(bio), 1, fPtr); 44 int tmp = getchar(); 45 //fflush(stdin); 46 printf("Any more records(y/n): "); 47 scanf("%c", &flag); 48 } 49 int clo = fclose(fPtr); (gdb) b 49 Breakpoint 3 at 0x12e2: file test6_12.c, line 49.
运行到断点停下:
(gdb) r Starting program: /home/nication/WORKM/stutyCode/cCode/recipesProblemSolution/chapter06/test6_12 Breakpoint 2, main (argc=1, argv=0x7fffffffd518) at test6_12.c:36 36 FILE *fPtr = fopen("agentsb.dat", "wb");
查看信息:
(gdb) n 37 if(fPtr != NULL){ (gdb) p *fPtr $1 = {_flags = -72539004, _IO_read_ptr = 0x0, _IO_read_end = 0x0, _IO_read_base = 0x0, _IO_write_base = 0x0, _IO_write_ptr = 0x0, _IO_write_end = 0x0, _IO_buf_base = 0x0, _IO_buf_end = 0x0, _IO_save_base = 0x0, _IO_backup_base = 0x0, _IO_save_end = 0x0, _markers = 0x0, _chain = 0x7ffff7f5e440 <_IO_2_1_stderr_>, _fileno = 3, _flags2 = 0, _old_offset = 0, _cur_column = 0, _vtable_offset = 0 '