Hi,
Does anyone know if there exists a C++ function equivalent of the VB Mid$. I have written my own version, but it keeps on adding extra chars on the end of my char *.
Cheers :confused:
Printable View
Hi,
Does anyone know if there exists a C++ function equivalent of the VB Mid$. I have written my own version, but it keeps on adding extra chars on the end of my char *.
Cheers :confused:
http://www.vbforums.com/forumdisplay.php?s=&forumid=9
Search the C++ forum. It has been asked several times.
And try to keep all C/C++ related topics in there, as well.
:)
Cheers....but it is as related to VB as C++!
Code:#include <iostream>
#include <string>
using namespace std;
int main() {
string s("counterproductive");
cout << s.substr(4, 10) << endl;
}
Excellent stuff, I'll give it a go.
Cheers!
Thanks for the suggestion, but couldn't get that method to work. Managed to solve the problem using:
char* result = strtok(char * searchString, char* delimiter)
;)
That's C not C++, and I would *very* strongly recommend against the use of strtok unless you really know what you're doing.
What wouldn't work about my method? That's the standard method with C++ strings.
I keep on getting the error message :
"E2294 Structure required on left side of . or .*"
It highlights the substr line.
You will have to excuse me, I am new to the world of C and C++. Here is the code that I am using if it helps:
char * getNoOfFiles(char * fheader)
{
char *noOfFiles;
noOfFiles = fheader.substr(1, 1);
return noOfFiles;
}
Thanks for the help.:)
p.s. whats wrong with strtok?
Thats because you are trying to do a substr to a string of chars, and chars dont have that built in function. Notice that parksie was using the std string class not char. You are better off using string class when ever possible, you will be better off.Quote:
Originally posted by VisBeg
I keep on getting the error message :
"E2294 Structure required on left side of . or .*"
It highlights the substr line.
You will have to excuse me, I am new to the world of C and C++. Here is the code that I am using if it helps:
char * getNoOfFiles(char * fheader)
{
char *noOfFiles;
noOfFiles = fheader.substr(1, 1);
return noOfFiles;
}
strtok is arcane, changes the input, and is non-reentrant. The latter is only important if you're using multiple threads.
.substr is for strings, not character pointers. You're doing all your work here in C, not C++. Use strings instead of char* pointers.
Sure. And it's related to PHP and JavaScript as you are trying to emulate the substr function/method. And to Perl and Java too of course. And about every language.Quote:
Cheers....but it is as related to VB as C++!
Sorry for the rant. I'm in a bad mood.
The reason I am using the 'old char *' stuff, is because I am having to communicate with a WIN CE device over a serial link. This means using some of the old cruddy methods, (as far as I can figure out anyway!)
Cheers for all of the suggestions.
:)
If the functions you're using require a const char* with the data to send, just use string::c_str() to access it. Much better to use the normal strings for any processing work.