-
Aug 15th, 2003, 08:03 AM
#1
Thread Starter
New Member
Mid$ - C++ equivalent - RESOLVED
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
Last edited by VisBeg; Aug 15th, 2003 at 08:56 AM.
-
Aug 15th, 2003, 08:12 AM
#2
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.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Aug 15th, 2003, 08:16 AM
#3
Thread Starter
New Member
Cheers....but it is as related to VB as C++!
-
Aug 15th, 2003, 08:24 AM
#4
Monday Morning Lunatic
Code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s("counterproductive");
cout << s.substr(4, 10) << endl;
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 15th, 2003, 08:30 AM
#5
Thread Starter
New Member
Excellent stuff, I'll give it a go.
Cheers!
-
Aug 15th, 2003, 08:55 AM
#6
Thread Starter
New Member
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)
-
Aug 15th, 2003, 09:15 AM
#7
Monday Morning Lunatic
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 refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 15th, 2003, 09:43 AM
#8
Thread Starter
New Member
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?
-
Aug 15th, 2003, 10:33 AM
#9
Frenzied Member
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;
}
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.
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com
-
Aug 15th, 2003, 10:34 AM
#10
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 18th, 2003, 04:38 AM
#11
Cheers....but it is as related to VB as C++!
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.
Sorry for the rant. I'm in a bad mood.
All the buzzt
CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Aug 18th, 2003, 10:11 AM
#12
Thread Starter
New Member
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.
-
Aug 19th, 2003, 05:55 PM
#13
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|