C + + divides the string into two dimensional word groups, 2 kinds of segmentation methods __c++

Source: Internet
Author: User

This experiment is to read a string in the INI file, the string is the form of a two-dimensional array. Method One

First I use the strtok_s;
Note:

The use of 1.strtok_s
Function prototypes:char *strtok_s (char *strtoken, const char *strdelimit, char **buf);
This function guarantees security by storing the remaining strings in the BUF variable rather than in the static variable.
The use of 2.strtok
Function Prototypes: char * strtok (char str, const char delimiters);
Parameters: str, string to be segmented (c-string); delimiters, delimiter string.
3. Difference:
strtok_s support Multithreading , Strtok can only be a single thread .

The files in INI are:

[Matrix]
data = 0, 0.3, 0.35, 0.40, 0.45, 0.5, 0.55, 0.60@ 20, 0.378, 0.421, 0.46, 0.496, 0.529, 0.559, 0.585@ 30, 0.261, 0.294, 0. 326, 0.357, 0.388, 0.419, 0.448@ 40, 0.183, 0.208, 0.232, 0.258, 0.285, 0.311, 0.339@ 45, 0.155, 0.177, 0.198, 0.22, 0.245 , 0.272, 0.295@ 50, 0.135, 0.154, 0.172, 0.192, 0.214, 0.236, 0.261@ 60, 0.106, 0.121, 0.136, 0.152, 0.166, 0.189, 0.210@ 70, 0.086, 0.098, 0.111, 0.123, 0.316, 0.154, 0.172@ 80, 0.074, 0.085, 0.096, 0.106, 0.117, 0.133, 0.149@

The code to run under VS2013 is:

#include <windows.h>
#include <string.h>
#incldue <iostream.h>

    Char data[500];
    Char ii1[15][100] = {0};
    Double C[colum_data][row_data] = {0};//row_data, Colum_data is its own defined
    int icount = 0,jcount = 0;
    char *ptr1;
    Char *ptr


    :: Getprivateprofilestringa ("Matrix", "Data", "" ", data, sizeof (data)-1," INI storage location ");
    PTR = strtok_s (data, "@", &p);     strtok_s can generate dynamic link libraries
    while (PTR!= NULL)
    {
        strcpy_s (Ii1[icount], ptr);
        Division The data again
        ptr1 = strtok_s (PTR, ",", &P1);     
        while (ptr1!= NULL)
        {
            strcpy_s (ii1[jcount), ptr1);
            C[icount][jcount] = atof (Ii1[jcount]);
            jcount++;
            PTR1 = strtok_s (NULL, ",", &P1);
            if (Jcount = = row_data) jcount = 0;
        }
        icount++;
        ptr = strtok_s (NULL, "@", &p);
    }
Method Two

Because strtok_s cannot operate on VC6.0, VC6.0 only supports strtok, while Strtok only supports single-threaded . Therefore, we have to read it in a different way.

[Matrix]
data = 0, 0.3, 0.35, 0.40, 0.45, 0.5, 0.55, 0.60@ 20, 0.378, 0.421, 0.46, 0.496, 0.529, 0.559, 0.585@ 30, 0.261, 0.294, 0. 326, 0.357, 0.388, 0.419, 0.448@ 40, 0.183, 0.208, 0.232, 0.258, 0.285, 0.311, 0.339@ 45, 0.155, 0.177, 0.198, 0.22, 0.245 , 0.272, 0.295@ 50, 0.135, 0.154, 0.172, 0.192, 0.214, 0.236, 0.261@ 60, 0.106, 0.121, 0.136, 0.152, 0.166, 0.189, 0.210@ 70, 0.086, 0.098, 0.111, 0.123, 0.316, 0.154, 0.172@ 80, 0.074, 0.085, 0.096, 0.106, 0.117, 0.133, 0.149@

Solving method

#include <windows.h>
#include <string.h>
#incldue <iostream.h>

    Char data[500];
    Char iii[15][100] = {0};
    Double C[colum_m][row_m] = {0};
    int icount = 0,jcount = 0;
    char *ptr1;
    Char delims[] = "@";


    Load the M_c from Agd.ini
    :: Getprivateprofilestringa ("Matrix", "Data", "" ", data, sizeof (data)-1," INI stored directory ");
    char* buf = data;
    char* result = NULL;
    while (result = Strstr (buf,delims))!=null)
    {
        result_c[0] = ';
        PTR1 = Strtok (buf, ",");
        while (ptr1!= NULL)
        {
            strcpy (iii[icount), ptr1);
            C[icount][jcount] = atof (Iii[icount]);
            jcount++;
            PTR1 = Strtok (NULL, ",");
            if (Jcount = = row_m) jcount = 0;
        }
        icount++;
        BUF = result + strlen (delims);
    }

This method has a good effect on solving the VC6.0 of strtok_s. Thus recorded in this.

Reprint please mark: Http://blog.csdn.net/huang1024rui

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.