user-0 提问: C语言按顺序执行写入文本语句,但写入文本的内容却不按顺序（有时顺序对，有时不对）

回复:
user: user-0
content: file1文件不用考虑

user: user-0
content: 引用 2 楼 czarten 的回复:贴代码 六个字补丁 就是一抓包程序，每抓一包分析，将分析信息写入文本txt中，vc++,winpcap抓包 部分代码 void ethernet_protocol_packet_callback(u_char *argument, const struct pcap_pkthdr *packet_header, const u_char *packet_content)      {         u_short ethernet_type;          /* 以太网类型 */          struct ether_header *ethernet_protocol;          /* 以太网协议变量 */          u_char *mac_string;          /* 以太网地址 */          static int packet_number = 1;          /* 数据包个数，静态变量 */       file1=fopen("H:\\example1.txt","at+"); 	file=fopen("H:\\example.txt","at+");     printf("**************************************************\n");          printf("捕获第%d个网络数据包\n", packet_number);  	fprintf(file1,"%d,",packet_number); 	fprintf(file,"捕获第%d个网络数据包\n",packet_number);     printf("捕获时间:\n");          printf("%s", ctime((const time_t*) &packet_header->ts.tv_sec));          /* 获得捕获数据包的时间 */          printf("数据包长度:\n");          printf("%d\n", packet_header->len);          printf("--------   以太网协议    --------\n");     ethernet_protocol = (struct ether_header*)packet_content;          /* 获得以太网协议内容 */          printf("类型:\n");          ethernet_type = ntohs(ethernet_protocol->ether_type);          /* 获得以太网类型 */          printf("%04x\n", ethernet_type);          switch (ethernet_type) /* 根据以太网类型判断 */          {      	case 0x0800:      		printf("上层协议为IP协议\n");  		break;      	case 0x0806:      		printf("上层协议为ARP协议\n"); 		break;      	case 0x8035:      		printf("上层协议为RARP协议\n");  		break;      	default:      		break;          }          printf("源MAC地址: \n");      mac_string = ethernet_protocol->ether_shost;  	printf("%02x:%02x:%02x:%02x:%02x:%02x\n", *mac_string, *(mac_string + 1), *(mac_string + 2), *(mac_string + 3), *(mac_string + 4), *(mac_string + 5)); 	fprintf(file,"源MAC地址:%02x:%02x:%02x:%02x:%02x:%02x\n", *mac_string, *(mac_string + 1), *(mac_string + 2), *(mac_string + 3), *(mac_string + 4), *(mac_string + 5)); 	/* 获得源以太网地址 */          printf("目的MAC地址: \n");      mac_string = ethernet_protocol->ether_dhost;          printf("%02x:%02x:%02x:%02x:%02x:%02x\n", *mac_string, *(mac_string + 1), *(mac_string + 2), *(mac_string + 3), *(mac_string + 4), *(mac_string + 5));  	fprintf(file,"目的MAC地址: %02x:%02x:%02x:%02x:%02x:%02x\n", *mac_string, *(mac_string + 1), *(mac_string + 2), *(mac_string + 3), *(mac_string + 4), *(mac_string + 5));     /* 获得目的以太网地址 */          switch (ethernet_type)          {      	case 0x0806:      		arp_protocol_packet_callback(argument, packet_header, packet_content);      		break;      		/* 上层协议为ARP协议，调用分析ARP协议的函数，注意参数的传递 */      	case 0x0800:      		ip_protocol_packet_callback(argument, packet_header, packet_content);      		break;      		/* 上层协议为IP协议，调用分析IP协议的函数，注意参数的传递 */      	default:      		break;          }          printf("**************************************************\n");          packet_number++;       printf("捕获数据包内容:\n"); 	for(int i=0;i<packet_header->len;i++) 	{ 		printf("%02x ",*(packet_content+i)); 		fprintf(file,"%02x",*(packet_content+i)); 	}  	fprintf(file,"\n\n",sizeof("\n\n")); 	printf("\n"); 	fclose(file1); 	fclose(file); } main() { 	const char user[] = "root";         //username       const char pswd[] = "123456";    //password       const char host[] = "localhost";    //or"127.0.0.1"       const char table[] ="test";       //database       unsigned int port = 3306;           //server port               mysql_init(&myCont);       if(mysql_real_connect(&myCont,host,user,pswd,table,port,NULL,0))       {           cout<<"connect succeed!"<<endl;           mysql_query(&myCont, "SET NAMES GBK");     //设置编码格式,否则在cmd下无法显示中文  	 	}	  	pcap_if_t *alldevs;//设备列表 	pcap_if_t *d; 	int inum; 	int i=0; 	pcap_t *adhandle;//PCAP源口 	char errbuf[PCAP_ERRBUF_SIZE];     /* 获取本机设备列表 */     if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)     {         fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);         exit(1);     }     /* 打印列表 */     for(d=alldevs; d; d=d->next)     {         printf("%d. %s\n", ++i, d->name);         if (d->description)             printf(" (%s)\n", d->description);         else             printf(" (No description available)\n");     }     if(i==0)     {         printf("\nNo interfaces found! Make sure WinPcap is installed.\n");         return -1;     }     printf("Enter the interface number (1-%d):\n",i);     scanf("%d", &inum);     if(inum < 1 || inum > i)     {         printf("\nInterface number out of range.\n");         /* 释放设备列表 */         pcap_freealldevs(alldevs);         return -1;     }     /* 跳转到选中的适配器 */     for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);     /* 打开设备 */ 	if ( ( 		adhandle= pcap_open(d->name,          // 监听的适配器名称 		65536,            // 65535保证能捕获到不同数据链路层上的每个数据包的全部内容 		PCAP_OPENFLAG_PROMISCUOUS,    // 混杂模式 		1000,             // 读取超时时间 		NULL,             // 远程机器验证 		errbuf       // 错误缓冲池 		) ) == NULL) 	{ 		fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name); 		/* 释放设备列表 */ 		pcap_freealldevs(alldevs); 		return -1; 	}     printf("\n正在监听 %s...\n", d->description);     /* 释放设备列表 */     pcap_freealldevs(alldevs);     /* 开始捕获 */     pcap_loop(adhandle,5, ethernet_protocol_packet_callback,NULL);//用pcap_open监听的抓包端口      pcap_close(adhandle); /*	int res; 	res=mysql_query(&myCont,"LOAD DATA LOCAL INFILE 'H://example1.txt' INTO  table packet1  FIELDS TERMINATED BY ',' ENCLOSED BY '' LINES TERMINATED BY '\n';"); 	if(!res) 	{ 		printf("Inserted id number %lu rows\n",(unsigned long)mysql_affected_rows(&myCont)); 	}else 	{ 		fprintf(stderr,"Insert error %d: %s\n",mysql_errno(&myCont),mysql_error(&myCont)); 	} 	mysql_close(&myCont);//断开连接*/     return 0; }

user: user-0
content: 引用 2 楼 czarten 的回复:贴代码 六个字补丁 代码太大了 传不上去

user: user-1
content: 贴代码 六个字补丁

user: user-2
content: 先去google “提问的智慧”