Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to find a string within a string and its not returning anything.....
C++
if (client.SendHttpRequest())
    {
        wstring httpResponseHeader = client.GetHttpResponseHeader();
        string str ("wstring httpResponseHeader");
		string str2 ("Content=");
		size_t found;
		found=str.find(str2);
		printf("\n%s\n\n",found);
		//wstring httpResponse = client.GetHttpResponse();
        wprintf(L"HTTP Response Header:\n%s\n\n", httpResponseHeader.c_str());
        
		//wprintf(L"HTTP Response:\n%s\n\n", httpResponse.c_str());
    }
Posted

What do you expect to find? The string your searching in is "wstring httpResponseHeader" and you're searching for "Content=" which is not part of that string. Then you store the result of the find member function in the variable found. Note that this value is a position value, and in this case will return the predefioned constant string::npos (which by the way is not equal to 0).

The reason you don't see any useful result from your printf statement however is entirely different: you provide a format string that expects a variable of type string (format specifier %s), but you provide a value of type size_t, which of course doesn't fit. Unfortunately printf does not (and in fact can not) do any type check, so it tries to convert string::npos into a char pointer, which of course results in something unexpected. Ahhh, the wonders of type-unsafe C functions...

If you want some type-safe output, use iostrem, like this:
C++
#include <iostream>
//...
   std::cout << std::endl << "find result: " << found << std::endl;

Note: std::endl is the equivalent of a line break; the main difference to '\n' is that it is guaranteed to work on any operating system, inlcuding those that require an additional '\r' (carriage return).

P.S.: check this out: http://www.cplusplus.com/reference/string/string/find/[^]
 
Share this answer
 
v2
Comments
Member 7766180 24-Aug-11 12:53pm    
Thank you for the information. I thought wstring httpresponseHeader was returning a string, and I could search that. If I can't search that, then how can I pull Content-Type from the results? Thank you I appreciate your help.
Sergey Alexandrovich Kryukov 24-Aug-11 19:08pm    
I suggest you formally accept this correct answer (green button).
My 5.
--SA
Stefan_Lang 25-Aug-11 4:40am    
There are various ways to convert strings from one representation to another. fortunately the standard template library that contains string and wstring can already deal with this almost naturally. All you need is one of the special constructors or copy functions that copy from a source of one type into a variable of another, e. g.
wstring ws= L"Hello";
string s(ws.begin(), ws.end()); // constructor that takes a range
or
wstring ws= L"Hello";
string s;
copy(ws.begin(), ws.end(), s.begin()); // copies a range to another location

In both of the above cases the type of elements stored in the source range (in this case wide chars) will be automatically converted into the correct type (single byte char) in the target location.

I think your error was that you thought an object of type string (or any other type) can be initialized by passing the name of another object as a string. This does not work in C++. Never! Instead you directly assign variables (without quotes) or pass them as an initializer. The moment you use double quotes the enclosed string will be treated as 'meaningless' text (meaningless to the program that is, unless you yourself actually write code to analyze the string and act upon its contents)
Member 7766180 25-Aug-11 16:09pm    
Thank you Stefan, I appreciate the answer and thought put into it. I will try it now.
You should deal with another hobby or learning the basics of programming. Buy you a good book or check out the tons of examples in the WorldWideWeb. Why you are starting with http protocol? Nobody will help you with your stupid repeating questions.
Regards.
 
Share this answer
 
Comments
Stefan_Lang 24-Aug-11 11:45am    
While you are probably right in your assessment of the programming skills of the poster, there is no reason to be harsh about it. Everybody has to start somewhere, so if you have a good link to provide, post it, or else there is no point in posting a 'solution'.
mbue 24-Aug-11 11:52am    
He should start with a simple exercise. And should not spam many times a day the same question.
btw: this is a solution: "By you a book", "Check out simple examples", "Learn the basics", "Use the web to get examples"!
Regards.
Member 7766180 24-Aug-11 12:49pm    
That was very rude on your part. I'm trying to learn this and there is no reason to be abusive. Besides you obviously don't think that I have been searching? I use codeproject as a last resource after many days of searching, and my question is hardly spam! It's a legitamite problem I am having. What is your solution?
Emilio Garavaglia 24-Aug-11 13:48pm    
The problem is the way you are approaching the problem is wrong. There are many solution, but all of them presume to completely rewrite your code (that look as a random messing-up of not understood concepts). A more proper approach is: you want to process HTML, but this requires understanding of networking, socket programming and string manipulation. Ok. Now forgot HTML for a while, and start studying those concept one by one with example proper of their own domains. Study how C++ handle and manage stings. Study how C++ handles I/O. And you will discover you have to understand operator overloading and templates. Stop a while, and start from them. Then pass to the C++ standard library: strings, I/O, containers and iteration. Then study the HTML specs. Only at that point start working on it. Whatever other help is misleading, and will give you even more confusion. Ha... also understand that C++ is not C. Whether it can call old C functions, there are better and more robust way to do certain things. Forget printf: C++ I/O is different.
Member 7766180 24-Aug-11 14:07pm    
Thank ypu for your sound advice. Your right, I need to get rid of printf and use c++ I/O. The whole thing is I am getting what I want using WSA but I have have this last problem of getting Content-Type from an http header where I can actually refrence it to run logic from it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900