Results 1 to 13 of 13

Thread: Mid$ - C++ equivalent - RESOLVED

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Location
    England
    Posts
    13

    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.

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Location
    England
    Posts
    13
    Cheers....but it is as related to VB as C++!

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Location
    England
    Posts
    13
    Excellent stuff, I'll give it a go.


    Cheers!

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Location
    England
    Posts
    13
    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)


  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Location
    England
    Posts
    13
    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?

  9. #9
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    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


  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  12. #12

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Location
    England
    Posts
    13
    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.


  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width