Thread: Problem returning a string

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    23

    Problem returning a string

    I keep having problems when I'm returning a string from my sub. It keeps printing:
    CPU Type: Auth(????VB
    CPU Model: AMD
    when I print.

    Here is my code:
    Code:
    char * GetCPUType()
    {
        FILE* fp;
      char buffer[1024];
      size_t readBytes;
      char* CPUTypeMatch;
      char* CPUType;
    
      /* Read /proc/cpuinfo */
      fp = fopen ("/proc/cpuinfo", "r");
      readBytes = fread (buffer, 1, sizeof (buffer), fp);
      fclose(fp);
    
      /*quit if nothing is read */
      if(readBytes==0 || readBytes == sizeof(buffer))
        return EXIT_SUCCESS;
      buffer[readBytes] = '\0';
    
      /* Locate the CPU Type */
      CPUTypeMatch = strstr(buffer,"vendor_id");
    
      sscanf (CPUTypeMatch,"vendor_id : %s", &CPUType);
      
      return CPUType;
    
    }
    
    int main(int argc, char *argv[])
    {
      char* CPUType;
      char* CPUModel;
      
      CPUType = GetCPUType();
      CPUModel = GetCPUModel();
      printf("CPU Type: %s \n", &CPUType);
      printf("CPU Model: %s \n", &CPUModel);
    
      return EXIT_SUCCESS;
    }
    Thank you!

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    The pointer you are returning does not have a valid size set aside for the string. So you are printing valid data for some other process.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    23
    Still no go.. I added a =malloc(100); in my code after the declarations. So my code in my main and bassically whereever else I had it written looks like this:

    Code:
    char* CPUType=malloc(100);
    It still is printing out the same thing... any more help would be greatly appreciated. Thank you!

  4. #4
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Your pointer is pointing to nothing. You put the data in a different buffer.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    23
    I'm sorry I guess I don't understand... could you please explain a little more..

    thank you

  6. #6
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    You are using sscanf(buffer, const char*, arg_list); You are using your inteded buffer in the arg_list position. Reverse the two char* variables.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    23
    I don't think the sscanf is my problem. If I print directly from my Get sub it works fine, but I don't want to to that. I want the information to be printed from my Main.

  8. #8
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    What I am telling you is the pointer to char (char*) you are returning is not in the recieving buffer position in the argument list past to sscanf(). You need to write it like this:
    Code:
    // your code is of which CPUType is not pointing to anything and
    // you are then expecting a string or char array and then you are
    // giving it the address of the pointer CPUType
    sscanf (CPUTypeMatch,"vendor_id : %s", &CPUType);
    
    // should be
    sscanf (CPUType, "vendor_id : %s", CPUTypeMatch);
    That should give you what you are looking to recieve. Just make sure that CPUType is pointing to a block of memory first.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    23
    That still doesn't work ... and now it just prints
    CPU Type: ???????VB

    I think my sscanf order is fine... Like I said before it works fine if I print in my getCPUType sub, but when I try to return it to my main it does not display correctly. Furthermore, if I move all the code to my main and print it works fine, but I don't want to do that. I want to call this a few times just by calling the sub. Do you or anyone else have any other suggestions. I think my problem is with returning a string... or in C I guess it's a char pointer.

    Thank you.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > sscanf (CPUTypeMatch,"vendor_id : %s", &CPUType);
    sscanf (CPUTypeMatch,"vendor_id : %s", CPUType);

    > printf("CPU Type: %s \n", &CPUType);
    printf("CPU Type: %s \n", CPUType);

  11. #11
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    My prefered way to format a string would be to use sprintf() it workes just like printf only it writes to the buffer. It is much better to use that sscanf().
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by velius
    My prefered way to format a string would be to use sprintf() it workes just like printf only it writes to the buffer. It is much better to use that sscanf().
    sscanf and sprintf have entirely different uses. You cannot simply say one is better than the other. One reads from a buffer, the other writes to a buffer.

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

  13. #13
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    You are completely misusing sscanf. This should be quite straightforward code. Try this...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char * get_word( void );
    
    int main()
    {
      char main_word[50];
    
      strcpy( main_word, get_word() );
    
      printf( "\n%s", main_word );
    
      sprintf( main_word, "Sub_word: %s'", get_word() );
      printf( "\n%s", main_word );
    
      printf( "\n%s\n\n", get_word() );
    
      return 0;
    }
    
    char *
    get_word()
    {
      char sub_word[50];
    
      strcpy( sub_word, "Easy" );
    
      return( sub_word );
    }
    There is no such thing as a humble opinion

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Morgan
    You are completely misusing sscanf. This should be quite straightforward code. Try this...

    Code:
    char *
    get_word()
    {
      char sub_word[50];
    
      strcpy( sub_word, "Easy" );
    
      return( sub_word );
    }
    You realize of course that you're returning the address of an array that goes out of scope when this function exits, right? As such, you end up pointing to an invalid memory location. Best to make it static, or use a pointer and malloc it.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Replies: 2
    Last Post: 06-21-2006, 04:23 AM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. Problem with string output...
    By Lau in forum C Programming
    Replies: 6
    Last Post: 11-20-2003, 10:51 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM