分裂字符数组

时间:2014-12-05 22:51:25

标签: c++ arrays char

我有一个char **names数组,基本上存储文件中的名称。

这是我的.txt文件

Mike, Sam, Stuart
Andre, Williams, Phillips
Patels, Khan, Smith

基本上,我想在,字符之前拆分和存储名称。 例如,Mike, Sam, Stuart将成为......

newName[0] = Mike
newName[1] = Sam
newName[2] = Stuart

我有这样的事情......

for (int i=0; i<3; i++)
{
  for (int j=60, j>0; j--)
  {
     if(names[i][j] == ',')
     {
        cout << j << endl; //THIS PRINTS OUT THE POSITION. HOW CAN I STORE THE POSITION AND DO SOMETHING?
     }
  }
}

如果有人可以帮我处理我的代码,我会很感激,这是正确的方向。我不想使用任何vectors

我试图存储这些学生的分数,但我想将其添加到double *marks[2]数组。

这是我的.txt文件......

69.9, 56.5
29.8, 20.0
35.6, 45.0

这是我的代码......

char **values;
char * pch;
pch = strtok (values[i], " ,");
while (pch != NULL)
{
    sscanf(pch, "%f, %f", &marks[i][0], &marks[i][1]);
    pch = strtok (NULL, " ,");
}

我收到了1.28277e-3071.96471e+257

等随机值

4 个答案:

答案 0 :(得分:1)

查看strtok命令,它对你很有帮助。

此代码查找连字符并打印内容...将其更改为逗号

#include <string.h>
#include <stdio.h>

int main()
{
   const char str[80] = "This is - www.tutorialspoint.com - website";
   const char s[2] = "-";

   char * newName[100]; /* at most 100 names */
   int iCurName = 0;


   char *token;

   /* get the first token */
   token = strtok(str, s);

   /* walk through other tokens */
   while( token != NULL ) 
   {
      printf( " %s\n", token );
      newName[iCurName] = malloc (char *) (strlen(token) + 1);
      strcpy(newName[iCurName],token);
      iCurrName ++;

      token = strtok(NULL, s);
   }

   return(0);
}

答案 1 :(得分:0)

使用函数strtok()将输入行拆分为标记;使用strcpy_s()将每个标记复制到名称缓冲区中。

注1 strtok()函数用'\0'字符替换每个分隔符,因此无法使用line声明const变量。如果输入缓冲区必须是常量,例如,如果要将整行用于其他内容,请在调用strtok()函数之前复制它。

注意2 :除了拆分输入行之外,您可能还需要修剪空间。

#define MAX_LINE_LENGTH 80
#define MAX_NAME_LENGTH 20
#define MAX_NAMES_PER_LINE 3

const char constInput = "Mike, Sam, Stewart";
char line[MAX_LINE_LENGTH];
strcpy_s(line, MAX_LINE_LENGTH, constInput);

char *separator = ",";

char newName[MAX_NAMES_PER_LINE][MAX_NAME_LENGTH];
int i = 0;
char *token = strtok(line, separator);
while ((i < MAX_NAMES_PER_LINE) && ((token = strtok(NULL, separator)) != NULL))
{
    strcpy_s(newName[i++], MAX_NAME_LENGTH, token);
}

答案 2 :(得分:0)

char newName[3][60];

for (int i=0; i<3; i++){
    int r=0, c=0;
    for (int j=0; j<60; j++){
        if(names[i][j] == ',' || names[i][j] == '\0'){
            newName[r++][c] = '\0';
            c = 0;
            if(names[i][j] == '\0'){
                cout << newName[0] << '\n'
                     << newName[1] << '\n'
                     << newName[2] << '\n' << endl;
                break;
            }
            while(names[i][++j] == ' ')
                ;
            --j;
        } else {
            newName[r][c++] = names[i][j];
        }
    }
}

#include <fstream>
#include <iostream>

using namespace std;

int main() {
    ifstream inf("data.txt");
    double marks[2];
    char ch;

    while(inf.good()){
        inf >> marks[0] >> ch >> marks[1];
        cout << "mark1:"  << marks[0] << endl;
        cout << "mark2:"  << marks[1] << endl;
    }
}

答案 3 :(得分:0)

我不知道这个功能是否足够快,但它是:

char** split_quotes(char *input, char separator = ' ', bool keep_quotes = false)
{
    if (&input && input)
    {
        size_t length = strlen(input);
        char **chunks = new char*[length];

        bool inQuotes = false;
        size_t count = 0, from = 0;

        for (size_t i = 0; i < length; i++)
        {
            if (input[i] == '"')
            {
                inQuotes = !inQuotes;
            }
            else if (input[i] == separator && !inQuotes)
            {
                size_t strlen = i - from;

                if (strlen > 0)
                {
                    if (!keep_quotes && input[from] == '"' && input[i - 1] == '"')
                    {
                        from++; strlen -= 2;
                    }

                    chunks[count] = new char[strlen + 1]();
                    strncpy(chunks[count], &input[from], strlen);
                    count++;
                }

                from = i + 1;
            }
        }

        if (from < length)
        {
            size_t strlen = length - from;

            if (!keep_quotes && input[from] == L'"' && input[length - 1] == L'"')
            {
                from++; strlen -= 2;
            }

            chunks[count] = new char[strlen + 1]();
            strncpy(chunks[count], &input[from], strlen);
            count++;
        }

        // Save chunks to result array //
        char **result = new char*[count + 1]();
        memcpy(result, chunks, sizeof(char*) * count);

        // free chunks //
        delete[] chunks;

        return result;
    }

    return NULL;
}

用法:

wchar_t **name = split_quotes(L"Mike,Donald,\"My Angel\",Anna", L',');
if (name)
{
    while (*name++)
    { 
        std::wcout << "Person: " << *name << std::endl;
    }
}