Sakshi
Sakshi

Reputation: 71

C++ file formatting and output to a file

Suppose I have a piece of code that prints something,like this,

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");
    }

The last line gives me output like

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 

I need to print the output of this line "printf("%.2x ", data[i]); " and then write this output into a file in C++. Can I please know how to do it?

Upvotes: 1

Views: 158

Answers (2)

Mats Petersson
Mats Petersson

Reputation: 129324

Use std::setw and std::hex to control width and how numbers are printed. std::dec will take you back to decimal (base 10) format. Use std::setfill('0') to fill with zero.

[I should point out that in all known implementations, fstream will call printf [or more likely snprintf or some similar function] internally, so it's not much gain, other than "it looks like C++" to use fstream over fprintf]

Upvotes: 1

Cisplatin
Cisplatin

Reputation: 2998

Look into the fstream standard library. An example of writing to a file can be found at here.

Upvotes: 1

Related Questions