To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
The ASP.NET Chart Control
Article :: Introduction to SharePoint Development in Visual Studio 2010
Part 10 of the Visual Basic .NET 2010 Express Tutorial Complete!
How to Use the Visual Studio Code Analysis Tool FxCop
Article :: Interview with Andrei Alexandrescu (Part 3 of 3)



Go Back   VBForums > Other Languages > C and C++

Reply Post New Thread
 
Thread Tools Display Modes
Old Aug 15th, 2003, 08:03 AM   #1
VisBeg
New Member
 
Join Date: Mar 03
Location: England
Posts: 13
VisBeg is an unknown quantity at this point (<10)
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.
VisBeg is offline   Reply With Quote
Old Aug 15th, 2003, 08:12 AM   #2
crptcblade
The Devil
 
crptcblade's Avatar
 
Join Date: Aug 00
Location: Quetzalshacatenango
Posts: 9,091
crptcblade is a jewel in the rough (200+)crptcblade is a jewel in the rough (200+)crptcblade is a jewel in the rough (200+)
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
crptcblade is offline   Reply With Quote
Old Aug 15th, 2003, 08:16 AM   #3
VisBeg
New Member
 
Join Date: Mar 03
Location: England
Posts: 13
VisBeg is an unknown quantity at this point (<10)
Cheers....but it is as related to VB as C++!
VisBeg is offline   Reply With Quote
Old Aug 15th, 2003, 08:24 AM   #4
parksie
Monday Morning Lunatic
 
parksie's Avatar
 
Join Date: Mar 00
Location: Mashin' on the motorway
Posts: 8,169
parksie is on a distinguished road (10+)
Code:
#include <iostream>
#include <string>

using namespace std;

int main() {
    string s("counterproductive");

    cout << s.substr(4, 10) << endl;
}
__________________
Quote:
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
parksie is offline   Reply With Quote
Old Aug 15th, 2003, 08:30 AM   #5
VisBeg
New Member
 
Join Date: Mar 03
Location: England
Posts: 13
VisBeg is an unknown quantity at this point (<10)
Excellent stuff, I'll give it a go.


Cheers!
VisBeg is offline   Reply With Quote
Old Aug 15th, 2003, 08:55 AM   #6
VisBeg
New Member
 
Join Date: Mar 03
Location: England
Posts: 13
VisBeg is an unknown quantity at this point (<10)
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)

VisBeg is offline   Reply With Quote
Old Aug 15th, 2003, 09:15 AM   #7
parksie
Monday Morning Lunatic
 
parksie's Avatar
 
Join Date: Mar 00
Location: Mashin' on the motorway
Posts: 8,169
parksie is on a distinguished road (10+)
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.
__________________
Quote:
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
parksie is offline   Reply With Quote
Old Aug 15th, 2003, 09:43 AM   #8
VisBeg
New Member
 
Join Date: Mar 03
Location: England
Posts: 13
VisBeg is an unknown quantity at this point (<10)
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?
VisBeg is offline   Reply With Quote
Old Aug 15th, 2003, 10:33 AM   #9
Technocrat
Frenzied Member
 
Technocrat's Avatar
 
Join Date: Jan 00
Location: I live in the 1s and 0s of everyones data streams
Posts: 1,024
Technocrat is an unknown quantity at this point (<10)
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;
}
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

Technocrat is offline   Reply With Quote
Old Aug 15th, 2003, 10:34 AM   #10
parksie
Monday Morning Lunatic
 
parksie's Avatar
 
Join Date: Mar 00
Location: Mashin' on the motorway
Posts: 8,169
parksie is on a distinguished road (10+)
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.
__________________
Quote:
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
parksie is offline   Reply With Quote
Old Aug 18th, 2003, 04:38 AM   #11
CornedBee
Kitten
 
CornedBee's Avatar
 
Join Date: Aug 01
Location: In a microchip!
Posts: 11,594
CornedBee is a glorious beacon of light (400+)CornedBee is a glorious beacon of light (400+)CornedBee is a glorious beacon of light (400+)CornedBee is a glorious beacon of light (400+)CornedBee is a glorious beacon of light (400+)CornedBee is a glorious beacon of light (400+)
Quote:
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.
CornedBee is offline   Reply With Quote
Old Aug 18th, 2003, 10:11 AM   #12
VisBeg
New Member
 
Join Date: Mar 03
Location: England
Posts: 13
VisBeg is an unknown quantity at this point (<10)
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.

VisBeg is offline   Reply With Quote
Old Aug 19th, 2003, 05:55 PM   #13
parksie
Monday Morning Lunatic
 
parksie's Avatar
 
Join Date: Mar 00
Location: Mashin' on the motorway
Posts: 8,169
parksie is on a distinguished road (10+)
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.
__________________
Quote:
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
parksie is offline   Reply With Quote
Reply

Go Back   VBForums > Other Languages > C and C++


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:29 PM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.