Question

I have written a c program which captures the packets from Ethernet in promiscuous mode and writes them into a .csv file.

It is as follows:

enter image description here

But I want my csv file as follows:

enter image description here

How can I do it? Code for writing to the csv file is :

int main()
{
/* declarations*/  
logfile=fopen(filename,"w+");
/*related stuffs*/
}
void print_udp_packet(unsigned char *Buffer , int Size)
{     
/*ip header length related codes*/     

  char str[] = "UDP";   
  fprintf(logfile , "Type:%s,SA:%d,DA:%d,UDP Length:%d,UDP Checksum:%d\n"  
  ,str,ntohs(udph->source),ntohs(udph->dest),ntohs(udph->len),ntohs(udph->check));

}

I have used \n and , for next row and column but I am not able to do as the above output?

[after editing as mentioned in the answer]

enter image description here

Was it helpful?

Solution

Unless I am missing something, simply adjust your fprintf statement to not include the column names. Then an initial logging line to generate the table headers.

int main()
{

    /* declarations*/  

    logfile=fopen(filename,"w+");
    if (logfile != NULL)
    {
         fprintf(logfile, "Type,SA,DA,UDP Length,UDP Checksum\n");
    }

    /*related stuffs*/
}

void print_udp_packet(unsigned char *Buffer , int Size)
{     
  /*ip header length related codes*/     


  fprintf(logfile , "%s,%d,%d,%d,%d\n",
                     "UDP",
                     ntohs(udph->source),
                     ntohs(udph->dest),
                     ntohs(udph->len),
                     ntohs(udph->check));

}

OTHER TIPS

I got it correct. Here it is: just removed \n from fprintf display statement

fprintf(logfile , "Type,Source Port,Destination Port,UDP Length,UDP Checksum"); 


fprintf(logfile , "\n%s,%d,%d,%d,%d\n","UDP",ntohs(udph->source),ntohs(udph-

>dest),ntohs(udph->len),ntohs(udph->check));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top