checked the boards and cant see this one

This is a discussion on checked the boards and cant see this one within the C Programming forums, part of the General Programming Boards category; Hey all, hope everyone is well. Happy new year...etc... Down to the serious stuff. I am writing a program to ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    16

    checked the boards and cant see this one

    Hey all, hope everyone is well. Happy new year...etc...

    Down to the serious stuff. I am writing a program to read a binary file into a structure and interperet it. The file is a captured ICMP packet. This program basically has to act like ethereal (for those who are familliar with it) and it being written in 64bit linux. Im not going to ask for code, just a few pointers. I can read the file into an array, but transferring the information into the structure has me stumped.

    Any ideas?

    Ive been looking through the other posts and come across copying structures out of a file but im not sure this applies to my problem as there is no structure to copy.

    Hopefully this is clearer than mud!

    thanks in advance

    Andy

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    16
    Thought id add some more, as a clearer example:

    Code:
    	
    typedef struct ip 
    {
    unsigned int ver:4;
    } packet;
    there is a snippet of structure


    Code:
    	packet*my_packet_pointer;
    	FILE*ptrFileln;
    	printf("Enter the name of the binary file to read\n");
    	scanf("%s",&cFileName[0]);
    	if((ptrFileln=fopen(cFileName, "r"))==NULL)
    		{
    			printf("Failed to open file\n");
    			return(1);
    		}
    
    while( fread( cBuffer,80,1,ptrFileln) != 0)
    reading from my file

    but moving the data from cBuffer and populating the structure is the bit i have no idea about.

    sorry about the untidy code, its a work in progress

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your structure is trying to be 4 bits in size. The smallest thing you can read is one byte at a time. Therefore, you'll have to mask off the portion of the byte you want, and stick it in your packet. There's a FAQ on bitwise operations you might want to read.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21