F-J-K
asked on
stringstream & getline() - How can it get a char pointer? C/C++
Ok experts, i'm stuck with this:
1. The code between *** 1 ***, works fine, but input.c_str() only converts to const char*,
i'm just curious! Is there a way i can make it convert to char*?
2. In *** 2 *** code, i get this: error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char *' (or there is no acceptable conversion). How can i handle this situation with stringstream()?
3. Assume i have this variable "char userInput[MAX_DATA];", what is the best what put data into it? Is it through cin>>, or ? - I think getline() - stringstream is a good one. Thus, i want to know how to do this on getline - stringstream unless if there is a better way to handle it.
4. Referring to *** 2 ****. Assume user inserted a number, so clientInput carries number. However, i need to ensure that user entered digits & not any other data. Is there any ready function in the C++ standard library that does the job?
5. Referring to *** 2 ****. If clientInput meant to carry an IP address & user lets say entered 121.33.200.22 - Is there a ready function that can check that user entered an IP address? so i can pass the variable to a function safely. I'm trying to avoid race conditions.
6. The two piece of codes below seems very similar. Sometimes i feel in the second code, i'm just adding an extra code after getline(). Sometimes i think it is more efficient to have the stringstream. The second one seems more robust, but in the first one i'm doing this input.c_str() which is not a significance difference, so is the second one is better in terms of security & filtering? Why?
7. Assume user inserted an IP address into clientInput. When i want to validate it, the algorithm of validation briefly will be as following:
a. convert the stream of chars into string
b. count the 4 dots
c. make sure other inputs are digits (excluding dot).
Do you have any suggestions?
I apologize for my long boring questions. Just answer what you know, one question or whatever you can share.
Best regards,
F.J.
1. The code between *** 1 ***, works fine, but input.c_str() only converts to const char*,
i'm just curious! Is there a way i can make it convert to char*?
2. In *** 2 *** code, i get this: error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char *' (or there is no acceptable conversion). How can i handle this situation with stringstream()?
3. Assume i have this variable "char userInput[MAX_DATA];", what is the best what put data into it? Is it through cin>>, or ? - I think getline() - stringstream is a good one. Thus, i want to know how to do this on getline - stringstream unless if there is a better way to handle it.
4. Referring to *** 2 ****. Assume user inserted a number, so clientInput carries number. However, i need to ensure that user entered digits & not any other data. Is there any ready function in the C++ standard library that does the job?
5. Referring to *** 2 ****. If clientInput meant to carry an IP address & user lets say entered 121.33.200.22 - Is there a ready function that can check that user entered an IP address? so i can pass the variable to a function safely. I'm trying to avoid race conditions.
6. The two piece of codes below seems very similar. Sometimes i feel in the second code, i'm just adding an extra code after getline(). Sometimes i think it is more efficient to have the stringstream. The second one seems more robust, but in the first one i'm doing this input.c_str() which is not a significance difference, so is the second one is better in terms of security & filtering? Why?
7. Assume user inserted an IP address into clientInput. When i want to validate it, the algorithm of validation briefly will be as following:
a. convert the stream of chars into string
b. count the 4 dots
c. make sure other inputs are digits (excluding dot).
Do you have any suggestions?
I apologize for my long boring questions. Just answer what you know, one question or whatever you can share.
Best regards,
F.J.
//********* 1 **********//
const char *clientInput;
string input;
getline(cin, input);
clientInput = input.c_str();
//******** 1 **********//
//******** 2 **********//
const char *clientInput;
string input;
getline(cin, input);
stringstream ss(input);
ss >> clientInput;
//******** 2 **********//
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> char* abc = const_cast<char *>(input.c_str());
Be careful with that ... There's a reason that c_str() returns a const char*. In general you should be very wary about casting away const-ness.
Be careful with that ... There's a reason that c_str() returns a const char*. In general you should be very wary about casting away const-ness.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ooops - race condition...
>> Ooops - race condition...
Aaaah, I was wondering why that was mentioned in the question ... Turns out F-J-K knew what was gonna happen heh ;)
Aaaah, I was wondering why that was mentioned in the question ... Turns out F-J-K knew what was gonna happen heh ;)
ASKER
Well done. It helped alot
>>i'm just curious! Is there a way i can make it convert to char*?
char* abc = const_cast<char *>(input.c_str());
>>2. In *** 2 *** code, i get this: error C2679: binary '>>' : no operator found which takes a right-hand operand of >> type 'const char *' (or there is no acceptable conversion). How can i handle this situation with stringstream()?
The problem is that
const char *clientInput;
is const and so can't be modified.