假設我有一段代碼,打印的東西,這樣,C++文件格式和輸出到文件
u_int packetCount = 0;
while (int returnValue = pcap_next_ex(pcap, &header, &data) >= 0)
{
// Show the packet number
printf("Packet # %i\n", ++packetCount);
// Show the size in bytes of the packet
printf("Packet size: %d bytes\n", header->len);
// Show a warning if the length captured is different
if (header->len != header->caplen)
printf("Warning! Capture size different than packet size: %ld bytes\n", header->len);
// Show Epoch Time
printf("Epoch Time: %d:%d seconds\n", header->ts.tv_sec, header->ts.tv_usec);
// loop through the packet and print it as hexidecimal representations of octets
// We also have a function that does this similarly below: PrintData()
for (u_int i=0; (i < header->caplen) ; i++)
{
// Start printing on the next after every 16 octets
if ((i % 16) == 0) printf("\n");
// Print each octet as hex (x), make sure there is always two characters (.2).
printf("%.2x ", data[i]);
}
// Add two lines between packets
printf("\n\n");
}
最後一行讓我像
Packet # 39
Packet size: 54 bytes
Epoch Time: 1084443432:328438 seconds
fe ff 20 00 01 00 00 00 01 00 00 00 08 00 45 00
00 28 0f 5c 40 00 80 06 91 d8 91 fe a0 ed 41 d0
e4 df 0d 2c 00 50 38 af ff f3 11 4c a9 48 50 10
24 14 31 71 00 00
Packet # 40
Packet size: 54 bytes
Epoch Time: 1084443445:216971 seconds
00 00 01 00 00 00 fe ff 20 00 01 00 08 00 45 00
00 28 c0 ad 40 00 2f 06 31 87 41 d0 e4 df 91 fe
a0 ed 00 50 0d 2c 11 4c a9 48 38 af ff f3 50 11
19 20 3c 64 00 00
Packet # 41
Packet size: 54 bytes
Epoch Time: 1084443445:216971 seconds
fe ff 20 00 01 00 00 00 01 00 00 00 08 00 45 00
00 28 0f 5f 40 00 80 06 91 d5 91 fe a0 ed 41 d0
e4 df 0d 2c 00 50 38 af ff f3 11 4c a9 49 50 10
24 14 31 70 00 00
Packet # 42
Packet size: 54 bytes
Epoch Time: 1084443457:374452 seconds
fe ff 20 00 01 00 00 00 01 00 00 00 08 00 45 00
00 28 0f 62 40 00 80 06 91 d2 91 fe a0 ed 41 d0
e4 df 0d 2c 00 50 38 af ff f3 11 4c a9 49 50 11
24 14 31 6f 00 00
我需要打印輸出該行的輸出「printf("%.2x ", data[i]);
」,然後將此輸出寫入C++中的文件。我可以請知道如何做到這一點?
是你的問題*「如何將十六進制數字寫入文件?」*?或者你想知道什麼? – JojOatXGME