Extracting a particular part from a message

Posted on 2005-04-28
Last Modified: 2013-11-13
I just want to extract a particular part of message using start and end boundaries.
For example
<start Boundary value > [filename="admissionnotices.html"]
<some text to be extracted>
<end Boundary value> [------=_Part_376_20547182.1114753430061]

Content-Type: audio/mpeg; name="SuttumVizhi.mp3"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
<start Boundary value > [filename="SuttumVizhi.mp3"]
<some text to be extracted>
<end Boundary value> [------=_Part_376_20547182.1114753430061]

in the above example  the start boundary will be "filename" and the end boundary will be "[------=_Part_376_20547182.1114753430061]"
now by giving these value as input i have to extract the content between these boundaries in C.

How to do this

Reply me

With Regards,

Question by:priyashree
    LVL 1

    Accepted Solution


    I think this is job for sscanf function. see for description and example

    hope this helps

    LVL 2

    Assisted Solution

    Assuming the filename and the part number changes, you can search for "[filename=", then search for the following "]", add 1 and you've got your start index.
    Search for "[------=_Part" and substract 1 and you got your end index.
    Then extract the string in between them.
    You can use the strstr function to serach for those substrings, and then the strncpy to get the substring.
    The code should look something like this:

    char* s = <your string>;
    char* substring;
    int start_index, end_index;

    // Get the start index.
    start_index = strstr(s, "[filename=");
    start_index = strstr(s + start_index, "]");

    // Get the end index.
    end_index = strstr(s, "[------=_Part");

    substring = calloc(end_index - start_index + 1, sizeof(char));
    strncpy(substring, start_index, end_index - start_index);

    Good luck!

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Suggested Solutions

    I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now