Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7647357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:26:30+00:00 2026-05-31T10:26:30+00:00

I have a Visual Studio 2008 C++ project where I need to parse a

  • 0

I have a Visual Studio 2008 C++ project where I need to parse a string to a structure of c-style character arrays. What is the most elegant/efficient way of doing this?

Here is my current (functioning) solution:

struct Foo {
    char a[ MAX_A ];
    char b[ MAX_B ];
    char c[ MAX_C ];
    char d[ MAX_D ];
};

Func( const Foo& foo );

std::string input = "abcd@efgh@ijkl@mnop";
std::vector< std::string > parsed;
boost::split( parsed, input, boost::is_any_of( "@" ) );

Foo foo = { 0 };
parsed[ 1 ].copy( foo.a, MAX_A );
parsed[ 2 ].copy( foo.b, MAX_B );
parsed[ 3 ].copy( foo.c, MAX_C );
parsed[ 4 ].copy( foo.d, MAX_D );

Func( foo );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:26:32+00:00Added an answer on May 31, 2026 at 10:26 am

    Here is my (now tested) idea:

    #include <vector>
    #include <string>
    #include <cstring>
    
    #define MAX_A 40
    #define MAX_B 3
    #define MAX_C 40
    #define MAX_D 4
    
    struct Foo {
        char a[ MAX_A ];
        char b[ MAX_B ];
        char c[ MAX_C ];
        char d[ MAX_D ];
    };
    
    template <std::ptrdiff_t N>
    const char* extractToken(const char* inIt, char (&buf)[N])
    {
        if (!inIt || !*inIt)
            return NULL;
    
        const char* end = strchr(inIt, '@');
        if (end)
        {
            strncpy(buf, inIt, std::min(N, end-inIt));
            return end + 1;
        } 
        strncpy(buf, inIt, N);
        return NULL;
    }
    
    int main(int argc, const char *argv[])
    {
        std::string input = "abcd@efgh@ijkl@mnop";
    
        Foo foo = { 0 };
    
        const char* cursor = input.c_str();
        cursor = extractToken(cursor, foo.a);
        cursor = extractToken(cursor, foo.b);
        cursor = extractToken(cursor, foo.c);
        cursor = extractToken(cursor, foo.d);
    }
    

    [Edit] Tests

    Adding a little test code

    template <std::ptrdiff_t N>
    std::string display(const char (&buf)[N])
    {
        std::string result;
        for(size_t i=0; i<N && buf[i]; ++i)
           result += buf[i];
        return result; 
    }
    
    int main(int argc, const char *argv[])
    {
        std::string input = "abcd@efgh@ijkl@mnop";
    
        Foo foo = { 0 };
    
        const char* cursor = input.c_str();
        cursor = extractToken(cursor, foo.a);
        cursor = extractToken(cursor, foo.b);
        cursor = extractToken(cursor, foo.c);
        cursor = extractToken(cursor, foo.d);
    
        std::cout << "foo.a: '" << display(foo.a) << "'\n";
        std::cout << "foo.b: '" << display(foo.b) << "'\n";
        std::cout << "foo.c: '" << display(foo.c) << "'\n";
        std::cout << "foo.d: '" << display(foo.d) << "'\n";
    }
    

    Outputs

    foo.a: 'abcd'
    foo.b: 'efg'
    foo.c: 'ijkl'
    foo.d: 'mnop'
    

    See it Live on http://ideone.com/KdAhO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an inherited project in Visual Studio 2008 for which I need to
I have an SQL report in a visual studio 2008 project. I need to
I have a Visual Studio 2008 C++ project that has support for using multiple
I have a Visual Studio 2008 C++ project that outputs a static library and
I have a bunch of Velocity template files in a Visual Studio 2008 project,
I have a .DLL that i include in my Visual Studio 2008 project. The
I have a visual studio 2008 solution that includes an asp.net-hosted remoting project in
I have a Visual Studio 2008 solution with two projects (a Word-Template project and
I have a custom build target in a visual studio 2008 c# project. Is
I have a database project (.dbp) open in Visual Studio 2008 as part of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.