0

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

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Beginner
  • 286
  • 4
  • 17
  • The easiest way? Pretty much what you seem to be doing anyway: Just write a clear-text .csv file. It's easy to create, easy to edit ... and easy to import into either Libre-Office or Excel. IMHO... – paulsm4 Nov 30 '13 at 07:01

2 Answers2

1

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

}
selbie
  • 100,020
  • 15
  • 103
  • 173
  • sir i have used your method but its displaying the columns twice!! I have uploaded a picture of it. how can i delete those columns?? – Beginner Nov 30 '13 at 09:47
  • You are likely re-printing the header with every row of data. – selbie Dec 01 '13 at 09:19
0

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));
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Beginner
  • 286
  • 4
  • 17