Thread: Reading csv file into a struct - not the correct values

  1. #1
    Registered User lantzvillian's Avatar
    Join Date
    Sep 2010
    Posts
    44

    Reading csv file into a struct - not the correct values

    Hi all,

    I've spend a couple of hours beating my head against the wall trying to read a csv back into a struct in my program. For some reason, I cannot get the values, which I wrote to the file read back into my struct correctly.

    Ideas?

    Struct:

    Code:
    typedef struct conn_pair {
        u_char  ether_shost[ETHER_ADDR_LEN];   /**source host address */
        u_char  ether_dhost[ETHER_ADDR_LEN];   /**destination host address */
        struct  in_addr ip_src;					/**source address */
        struct  in_addr ip_dst;  				/**dest address */
        u_short th_sport;               		/**source port */
        u_short th_dport;               		/**destination port */
        u_char  ip_p;                   		/**protocol */
        enum conn_state state;					/**connection state */
    } CONNECTION;
    Offending code:

    Code:
    	if (f == NULL) return -1; 
    		
    	u_char proto = 0;
    	char ether_shost;
    	u_char ether_dhost;
    	char srcIP;
    	char dstIP;
    	u_short srcPort = 0;
    	u_short dstPort = 0;
    	enum conn_state state = na;
    	struct ether_addr *tempeth = NULL;
    
    	char *rec;
        char *token;
        char *flds[8];
        int i;
     
        rec = malloc(2048);
        while (fgets(rec, 2048, f) != NULL) {
    		CONNECTION temp;
    	    
    	    rec[strlen(rec) - 1] = '\0';
            i = 0;
            token = strtok(rec, ",");
           
           while (token != NULL) {
    			
                flds[i] = token;
                i++;
                token = strtok(NULL, ",");
            }
            
            memcpy(&temp.ip_p,flds[0],sizeof(u_char));
            memcpy(&temp.ether_shost,(char *)ether_aton((char *)flds[1]),sizeof(u_char));
    		inet_pton(AF_INET, flds[2], &temp.ip_src);
    		temp.th_sport = ntohs(flds[3]);
    		memcpy(temp.ether_dhost,ether_aton((char *)flds[4]),sizeof(u_char[ETHER_ADDR_LEN]));
    		inet_pton(AF_INET,flds[5],&temp.ip_dst);
    		temp.th_dport = ntohs(flds[6]);
    		memcpy(&temp.state,flds[7],sizeof(int));
    		
    
    		
    		printf("\n0x%-2x %15s:%-5d -> ", \
                   temp.ip_p, \
                   inet_ntoa(temp.ip_src), \
                   ntohs(temp.th_sport));
    
            printf("%-15s:%-5d %u\n", \
                   inet_ntoa(temp.ip_dst), \
                   ntohs(temp.th_dport), \
                   temp.state \
                  );
    	}
    Input - don't worry about the MACs.

    11,0:15:6d:c4:27:4b,142.232.191.37,68,0:15:6d:c4:2 7:4b,142.232.191.37,67,5
    6,0:15:6d:c4:27:4b,74.125.53.125,35330,0:15:6d:c4: 27:4b,74.125.53.125,5222,3
    6,1c:c1:de:af:b0:ba,192.168.1.182,443,1c:c1:de:af: b0:ba,192.168.1.182,36577,3

    Output:

    0x31 142.232.191.37:52131 -> 142.232.191.37 :52166 8245
    0x36 74.125.53.125:52129 -> 74.125.53.125 :52166 8243
    0x36 192.168.1.182:52130 -> 192.168.1.182 :52166 8243

    As you can see, the IP addresses are working correctly, but for the life of me I cannot get the fields: ip_p, th_sport, th_dport and state are not working correctly.

    I'm guessing this is for a number of reasons - anyone know?

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    your first output 0x31 is the character '1'. 0x36 is the character'6'. you are copying a single character from your first token into your structure, but the input token (of the first line) is 2 characters long. if you want the numeric value, you need to take the string in flds[0] and convert it to a number using scanf or atoi.

  3. #3
    Registered User lantzvillian's Avatar
    Join Date
    Sep 2010
    Posts
    44
    You sir are awesome. Simple is - simple forgets.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    A runnable program would've been nice. But...

    Don't you want a size of ETHER_ADDR_LEN for you memcpy into ether_shost?

    And this is saying to copy 4 bytes from the string of chars that flds[7] points to, but that's not what you want.
    Code:
    memcpy(&temp.state,flds[7],sizeof(int));
    You want something like
    Code:
    sscanf(flds[7], "%d", &temp.state);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User lantzvillian's Avatar
    Join Date
    Sep 2010
    Posts
    44
    Ahh yes so what I did was this:

    Code:
            temp.ip_p = atoi(flds[0]);
            memcpy(&temp.ether_shost,(char *)ether_aton((char *)flds[1]),sizeof(u_char[ETHER_ADDR_LEN]));
            inet_pton(AF_INET, flds[2], &temp.ip_src);
            temp.th_sport = atoi(flds[3]);
            memcpy(temp.ether_dhost,ether_aton((char *)flds[4]),sizeof(u_char[ETHER_ADDR_LEN]));
            inet_pton(AF_INET,flds[5],&temp.ip_dst);
            temp.th_dport = atoi(flds[6]);
            temp.state = atoi(flds[7]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading values from text file
    By a.mlw.walker in forum C Programming
    Replies: 5
    Last Post: 01-14-2012, 06:02 PM
  2. Reading two values from a file as one value?
    By Flotonic in forum C Programming
    Replies: 4
    Last Post: 04-08-2011, 05:53 PM
  3. reading in a text file containing hex values
    By gaza2k1 in forum C Programming
    Replies: 34
    Last Post: 02-29-2008, 07:15 PM
  4. reading values from a file
    By megastar in forum C Programming
    Replies: 4
    Last Post: 06-25-2007, 02:08 AM
  5. Reading float values from a file??
    By neil_w_84 in forum C++ Programming
    Replies: 1
    Last Post: 02-23-2005, 08:48 AM