C/C++把字符串划分为二维字数组,2种分割方法

本实验是读取ini文件中的字符串,字符串是二维数组的形式。

方法一

首先我用的是strtok_s;
注:

1.strtok_s的用法
函数原型:char *strtok_s( char *strToken, const char *strDelimit, char **buf);
这个函数将剩余的字符串存储在buf变量中,而不是静态变量中,从而保证了安全性。
2.strtok的用法
函数原型:char * strtok (char str, const char delimiters);
参数:str,待分割的字符串(c-string);delimiters,分割符字符串。
3.区别:
strtok_s支持多线程,strtok只能是单线程

在ini里面的文件是:

[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@

在VS2013下运行代码是:

#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是自己定义的
    int icount = 0,jcount = 0;
    char *ptr1;
    char *ptr;


    ::GetPrivateProfileStringA("Matrix", "data", "", data, sizeof(data)-1, "ini的存放位置");
    ptr = strtok_s(data, "@", &p);     //strtok_s可生成动态链接库
    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);
    }

方法二

由于strtok_s无法在VC6.0上操作,VC6.0只支持strtok;而strtok只支持单线程。因此,我们得用另外的方法对其进行读取。

[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@

解决方法

#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存放的目录");
    char* buf = data;
    char* result = NULL;
    while((result = strstr(buf,delims))!=NULL)
    {
        result_c[0] = '\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);
    }

此种方法对解决VC6.0不能用strtok_s有很好的作用!因而在此记录。

转载请标注:http://blog.csdn.net/huang1024rui

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值