Urgent.Help needed

This is a discussion on Urgent.Help needed within the C Programming forums, part of the General Programming Boards category; Hi, any help with this would be great. Im new to C. Im using it on a Linux platform. Basically ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    8

    Urgent.Help needed

    Hi, any help with this would be great.

    Im new to C. Im using it on a Linux platform.

    Basically I have a file called frame.bin, its an IP structure. How do I go about reading this file and then print it as say a text file? Im not good with code either, so as much help as possible please. Cheers.

  2. #2
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    The first thing you should do is to read about "Forum guidelines"

    http://cboard.cprogramming.com/annou...ouncementid=51

    ....
    1. Don't use all caps
    2. Use descriptive subject lines
    3. Do not put URGENT!, or NEED HELP NOW, etc. in your title; it will not make people look at it any faster. Doing this makes many old time helpers on this board not look at the post at all.

    You should read(2) the frame.bin file into a buffer until reaching end of file, and then you can display the buffer ın the screen or write the buffer into another file...

    If the frame.bin is not a binary file (but it seems it is!), use fopen() to open the file and fread() or fscanf() to get data from the file.

    > ...its an IP structure....
    you mean something like
    Code:
    struct ip_hdr { ....
    bls ..
    bla..
    };
    then include the file and declare a struct of type ip_hdr. You can access the structure fileds then.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    8
    Sorry, i never read the rules prior to posting.
    As reguards to the .bin file, I created it using this program

    This is a program to use structures functions

    -------------------------------------------------

    Creating destination MAC adderss
    Creating source MAC adderss
    Type length field 0x800==IP
    frame complete
    [00 50 04 3C 9A 9E 00 0E A6 C5 7E 4B 08 00 ]
    [45 00 3C 00 98 65 00 00 80 01 2B 51 C0 A8 FA C7 C0 A8 FA F0 ]
    [08 00 31 5C 02 00 1A 00 FF FF FF 00 ]
    Writing to binary file


    All that info have been included in the frame.bin file. What i need to do is read it back and then print it. As i said before im a real novice, and although i understand what ur trying to get me to do, im just not sure how to get there step by step??

  4. #4
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Ok again the forum guidelines, see the 2. one!

    Code:
    int main()
    {
    ...
    FILE *fd;
    char buffer[SOMETHING];
    ...
    fopen("frame.bin","r");
    /* do some error checking */
    while (fgets(buffer,sizeof(buffer),fd)!=NULL)
    {
    ...
    fputs(buffer,stdout);
    }
    close(fd);
    return 0}
    You may also use read(2) and open(2) for this purpose, but the calls you use will be different (because you will not have a FILE * after open(2))

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If it's binary data, you'll want to open the file in binary mode and use fread.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    8
    How do you operate in binary mode?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Something like this.
    Code:
    void readfile(const char *filename)
    {
       FILE *file = fopen(filename, "rb");
       if ( file != NULL )
       {
          unsigned char buffer[8];
          size_t i, count;
           
          count = fread(buffer, sizeof *buffer, 6, file);
          if ( count == 6 )
          {
             fputs("Destination : ", stdout);
             for ( i = 0; i < 6; ++i )
             {
                printf("%02X", (unsigned)buffer[i]);
             }
             putchar('\n');
          }
          
          /* ... */
          
          fclose(file);
       }
       else
       {
          perror(filename);
       }
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Or using open(2) and read(2) works on binary files

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    8
    What does the (2) min brackets mean?

  10. #10
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Well, if you are using UNIX (linux, FreeBSD, etc..) there are several manula page sections. For example section 2 is reserved for system calls and section 3 is reserved for C calls.

    2 in the barckets means the manual page for open (or whatever) in section 2. Try "man open" and "man 2 open", you will see different pages.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    8
    oh rite, well im using the fedora core 4 and doing my programming in C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. lock needed in this scenario?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-25-2008, 07:22 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. semi-colon - where is it needed
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2003, 05:24 PM

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