Getting different output with printf and cout - C++

Viewed 13759

I have a string I am trying to print. when I used cout, it outputs perfectly but using printf leaves it mangled.

Here is the code:

int main ( int argc, char *argv[] )
{
    // Check to make sure there is a single argument
    if ( argc != 2 )
    {
        cout<<"usage: "<< argv[0] <<" <filename>\n";
        return 1;
    }

    // Grab the filename and remove the extension
    std::string filename(argv[1]);
    int lastindex = filename.find_last_of("."); 
    std::string rawname = filename.substr(0, lastindex);

    cout << "rawname:" << rawname << endl;
    printf("rawname: %s", rawname);

}

The cout gives me "rawname: file"
The printf gives me "rawname: " and then a bunch of squiggly characters

5 Answers
Related