简体   繁体   中英

C++: Weird std::cout error

I thought this would be a relatively simple thing to do: append "www.google.ie" with a trailing slash, and pre-pend it with "http://", resulting in a string with the value "http://www.google.ie/". No, this is not homework... (I know)

Now here is my code:

std::string line=split1[0];   //split1[0] is "Host: www.google.ie"
std::vector<std::string> split2;
boost::split(split2,line,boost::is_any_of(" "));
boost::erase_all(split2[1],"\n");
std::cout<<"split2[1]:"<<split2[1]<<std::endl;   //outputs www.google.ie ok
fURL="http://"+split2[1]+"/";
//fURL="http://www.google.ie/";   //this is what I want fURL to be!
std::cout<<std::endl;   //just for some testing
std::cout<<std::endl;
std::cout<<std::endl;
std::cout<<std::endl;
std::cout<<std::endl;
std::cout<<"fURL:"<<fURL<<std::endl;   //should output: http://www.google.ie/?

And here is my weird output:

 split2[1]:www.google.ie /URL:http://www.google.ie 

I have no idea where the '/' in '/URL:' is coming from. It's as if the trailing slash I specified is being tacked on to the front somehow. I really don't understand how this is possible...

Using g++ 4.5.2 on Linux Ubuntu.

Any insight greatly appreciated.

Many thanks in advance,

I guess that this line

//split1[0] is "Host: www.google.ie"

is something different than what you say. If you got it over http for example you would have

//split1[0] is "Host: www.google.ie\\r\\n"

which after deleting \\n is

//split1[0] is "Host: www.google.ie\\r"

Then fURL is

fURL="http://"+split2[1]+"/"; // http://www.google.ie \\r/

This

std::cout<<"fURL:"<<fURL<<std::endl

will print

fURL:http://www.google.ie

go to the first column (\\r) and print '/' overwriting first character 'f'

This is your code, put into a little program with just that one call to your code wrapped in a foo()-function. It works as you would expect, and does nothing weird like you are observing. If I run into a problem like you have I always write a little program with just that code that is "weird". As suggested by the others, there has to be something else, that is making things go wrong. Here, try it out:

#include <iostream>
#include <vector>
#include <string>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/erase.hpp>
using namespace std;

void foo(const char **split1)
{
   std::string line = split1[0];   //split1[0] is "Host: www.google.ie"
   std::vector<std::string> split2;

   boost::split(split2,line,boost::is_any_of(" "));
   boost::erase_all(split2[1],"\n");

   std::cout<<"split2[1]:"<<split2[1]<<std::endl;   //outputs www.google.ie ok

   string fURL="http://"+split2[1]+"/";
   //fURL="http://www.google.ie/";   //this is what I want fURL to be!

   std::cout<<std::endl;   //just for some testing
   std::cout<<std::endl;
   std::cout<<std::endl;
   std::cout<<std::endl;
   std::cout<<std::endl;
   std::cout<<"fURL:"<<fURL<<std::endl;   //should output: http://www.google.ie/?

}
int main()
{
    const char *split = "Host: www.google.ie";
    const char *split1[1];
    split1[0]  = split;

    foo(split1);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM