Thread: Having great difficulty with something that should be easy...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    2

    Having great difficulty with something that should be easy...

    Greetings and Salutations,

    I've been thrust into a situation at work where I need to do C programming. The problem is that I have never had any formal training in any kind of programming nor do I have the time to spend for the rest of the day to read a book as I have a deadline and this stupid (stupid on my part) problem is getting in the way. I am attempting to extract a value from a string and I cannot seem to get my code to work.

    Code:
    char cExhibitionID;
    cExhibitionID = "<data jsxid=\"jsxroot\" entityName=\"Exhibition\"><record jsxid=\"N26D698DD\" a1=\"299288\" a2=\"38\" a3=\"32\" a4=\"PCTESTEXHIBITION1\" a5=\"PC1\" a6=\"EX0000589\"><record jsxid=\"N7A004F5B\" a9="" a99=""><record jsxid=\"N55D0B4D8\" a10="" a100=""/></record></record></data&gt";
    char cString;
    
    sscanf(cExhibitionID, "<data jsxid=\"jsxroot\" entityName=\"Exhibition\"><record jsxid=\"N26D698DD\" a1=\"%s\" a2=\"38\" a3=\"32\" a4=\"PCTESTEXHIBITION1\" a5=\"PC1\" a6=\"EX0000589\"><record jsxid=\"N7A004F5B\" a9="" a99=""><record jsxid=\"N55D0B4D8\" a10="" a100=""/></record></record></data&gt", cString);
    
    lr_log_message("The value is %s", cString);
    I'm using a tool called VuGen and what I'm attempting to do is extract "299288" from the string passed into the cExhibitionID variable. Please do not worry about the lr_log_message. That function is specific to VuGen.

    In the sscanf I attempted to extract this value by putting a "%s" in place of the number thinking this would work. Clearly it does not. I've been at this for hours and I'm having great difficulty. If anyone can assist me with some ideas I would greatly appreciate it.

    Thank you.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    That code won't even compile.
    Code:
    char cExhibitionID;
    That's room for a single character.

    as is this
    Code:
    char cString;
    And you're in for a world of hurt trying to parse XML the way you are. I would suggest a higher-level language for this, or at least the use of something like libxml2.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Here, learn this also:
    Code:
    char *st = "This is a really long string, but in place of having it stretch all the way "
               "around the world, I'll just break it so that it looks somewhat better.\n";

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    2
    I can't believe it....I actually got it to work. Here's what I did:

    Code:
    Action()
    {
       
    char cExhibitionID[1024] = "<data jsxid=\"jsxroot\" entityName=\"Exhibition\"><record jsxid=\"N26D698DD\""
    "a1=\"299288 a2=\"38\" a3=\"32\" a4=\"PCTESTEXHIBITION1\" a5=\"PC1\" a6=\"EX0000589\""
    "><record jsxid=\"N7A004F5B\" a9="" a99=""><record jsxid=\"N55D0B4D8\" "
    "a10="" a100=""/></record></record></data&gt";
    
    char cString[1024];
    
    sscanf(cExhibitionID, "<data jsxid=\"jsxroot\" entityName=\"Exhibition\"><record jsxid=\"N26D698DD\" "
    "a1=\"%s \" a2=\"38\" a3=\"32\" a4=\"PCTESTEXHIBITION1\" a5=\"PC1\" a6=\"EX0000589\""
    "><record jsxid=\"N7A004F5B\" a9="" a99=""><record jsxid=\"N55D0B4D8\" a10="" "
    "a100=""/></record></record></data&gt", cString);
    
    lr_log_message("The value is %s", cString);
    
    return 0;
    }
    This returned the number that I needed.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    That doesn't make any sense for a lot of reasons, but whatever floats your boat I guess.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Looks like that string is missing a few backslashes. The bit with a9="" a99="" for example will actually just look like a9= a99= after the compile-time string concatenation takes place.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy String position question...
    By Striph in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:48 PM
  2. Seg fault in easy, easy code
    By lisa1901 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2007, 05:28 AM
  3. Java vs C to make an OS
    By WOP in forum Tech Board
    Replies: 59
    Last Post: 05-27-2007, 03:56 AM
  4. the great mfc returns
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 09-17-2001, 07:39 PM