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 8623475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:17:23+00:00 2026-06-12T07:17:23+00:00

I am trying to split a string by a specified delimiter according to this

  • 0

I am trying to split a string by a specified delimiter according to this example: http://msdn.microsoft.com/en-us/library/ftsafwz3(v=VS.90).aspx

My code compiles without errors in Visual C++ 2010, but when I want to run it, I get this error message:

Unhandled exception at 0x773a15de in Test.exe: 0xC0000005: Access violation reading location 0x00000000.

Here’s my code:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <regex>

using namespace std;

vector<char *> splitString(char in[])
{
vector<char *> parts(15);
char seps[]   = " ,\t\n";
char *next_token1 = NULL;
char *token1 = NULL;
token1 = strtok_s(in, seps, &next_token1);
while ((token1 != NULL))
{
    if (token1 != NULL)
    {
        token1 = strtok_s( NULL, seps, &next_token1);
                    //printf( " %s\n", token1 );
        parts.push_back(token1);
    }
}
return parts;
}

int main(int argc, char * argv[])
{
char string1[] =
    "A string\tof ,,tokens\nand some  more tokens";
vector<char *> parts=splitString(string1);
cout << parts[0] <<endl;
cout << parts[1] <<endl;
return 0;
}

It seems to be illegal that I try to display the vector’s elements, but why?

The vector’s capacity should be sufficient and a

printf( ” %s\n”, token1 );

in the while loop prints out the tokens!

  • 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-06-12T07:17:25+00:00Added an answer on June 12, 2026 at 7:17 am

    The use of vector is incorrect. It is constructed with 15 elements and then push_back() is used to add the strings which adds new elements after the initial 15. This means the first 15 elements are unassigned (null):

    std::cout << parts[0] << end; // parts[0] is null
    

    Either:

    • don’t preallocate elements at construction, or
    • use operator[] and not push_back() (add additional loop terminator to protect going beyond the end of the vector)

    (Consider changing to std::vector<std::string>.)

    Just to mention boost::split() that can produces a list of tokens (std::vector<std::string>) from an input string and permits specification of multiple delimiters.

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

Sidebar

Related Questions

Trying to split this string 主楼怎么走 into separate characters (I need an array) using
Im trying to split a string according to the following rules: words without around
I am trying to split a string in javascript . it works fine in
I'm trying to split a string on its punctuation, but the string may contain
I am trying to split a string in Javascript using a Regular Expression. My
I'm trying to split a string into a int list for further processing. But
i am trying to split the string to multilines when tweeting using the twitter
So I'm trying to split just a string I've read in through a csv
I am trying to split the single string in to two lines using \n
I have made a String[] array by using String.split(.) . I am now trying

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.