Help .. Conversion Problems

This is a discussion on Help .. Conversion Problems within the C++ Programming forums, part of the General Programming Boards category; Ok, this is really stupid and I'm kicking myself with the fact that I'm stuck on it. I'm prototyping a ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    3

    Help .. Conversion Problems

    Ok, this is really stupid and I'm kicking myself with the fact that I'm stuck on it. I'm prototyping a small program that moves back and forth though a directory of images by their number. Basically I just want to increase or decrease my integer Page_Num (which I have done here) then convert that number to a const char* array and append the file extension to it so that I can open the next file in sequence.


    Code:
    void page_forward(Fl_Widget*, void*)
        {
         test->uncache();           // Clear the image
    
         const char* buffer;        // String buffer
    
         Page_Num = Page_Num + 1;   // Increase the page count
    
         // String conversion here ....
    
         test = new Fl_GIF_Image(buffer);    //load the image cache
    
         img_butt->image(test);                    // assign the cache to the button
    
         window->redraw();                          // Force a redraw
        }
    Thanks ...
    Last edited by rivieracadman; 12-29-2004 at 01:21 PM. Reason: oops code tags ...

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I think you probably want sprintf() to use C syntax or C++ stringstreams.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    3

    Cool ...

    Quote Originally Posted by elad
    I think you probably want sprintf() to use C syntax or C++ stringstreams.

    Thanks, you put me on the right track. Here's what I come up with that works:

    Code:
    void page_forward(Fl_Widget*, void*)
        {
         test->uncache();                       // Clear the image
    
         const char*  buffer;                   // Const buffer
         char*        temp_buff;                //Char Buffer
         const char*  file_ext = ".gif";     //File Extension
         stringstream ss;
    
         Page_Num = Page_Num + 1;     // Increase the page count
    
         ss << Page_Num << file_ext;     //combine them in the stream
    
         string s = ss.str();     //convert to string
         sprintf(temp_buff, ss.str().c_str());     //convert to char array
         
         buffer = temp_buff;     //convert to const
    
         test = new Fl_GIF_Image(buffer);     //load the image cache
         img_butt->image(test);     // assign the cache to the button
    
         window->redraw();      // Force a redraw
        }

    It works well, but uses a little more memory then I like. It's also kind of lengthy. Is there a more efficiant way of doing this?

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Code:
    void page_forward(Fl_Widget*, void*)
        {
         test->uncache();                       // Clear the image
    
         const char*  file_ext = ".gif";     //File Extension
         stringstream ss;
    
         Page_Num = Page_Num + 1;     // Increase the page count
    
         ss << Page_Num << file_ext;     //combine them in the stream
    
         test = new Fl_GIF_Image(ss.str().c_str());     //load the image cache
         img_butt->image(test);     // assign the cache to the button
    
         window->redraw();      // Force a redraw
        }
    No need for all those conversions, just pass the stream's string's const char* to the function.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    3
    Wow! I didn't know you could pass a stream as a const. I'm going to have to pull out the book on that. Thanks for your help. Here's my final code:

    Code:
    void page_forward(Fl_Widget*, void*)  //## 
        {
         const char*   file_ext = ".gif"; // File extension
         stringstream  sstream;           // our string stream
    
         test->uncache();                 // Clear the image cache
    
         if (Page_Num < Max_Count_Images) //## Check and increment the image count
            {
             Page_Num = Page_Num + 1;     // Increase the page count
            }
         else Page_Num = 1;               //## or return it to one
         
         sstream << Page_Num << file_ext; // Add our items to the stream
    
         test = new Fl_GIF_Image(sstream.str().c_str()); // create a new instance and load the image cache
         img_butt->image(test);           // assign the cache to the button
    
         window->redraw();                // Force a redraw
        }
    Man I wish I had known that some time ago...

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Technically this:
    Code:
    foo(sstream.str().c_str());
    is the same as this:
    Code:
    string str = sstream.str();
    const char* buffer = str.c_str();
    foo(buffer);
    without all the extra temporary variables.

    The c_str() string function returns a const char pointer to its contents.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Problems with Break in C
    By AKalair in forum C Programming
    Replies: 6
    Last Post: 07-13-2008, 02:49 PM
  3. Bad output -- possible input buffer problems
    By jake123 in forum C Programming
    Replies: 8
    Last Post: 02-18-2008, 02:36 PM
  4. pointer conversion problems with a copy constructor
    By stanlvw in forum C++ Programming
    Replies: 8
    Last Post: 01-13-2008, 11:06 PM
  5. Problems: Operator overloading.
    By Dual-Catfish in forum C++ Programming
    Replies: 17
    Last Post: 06-18-2002, 06:38 PM

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