为什么在split函数的此代码中出现分段错误?

时间:2019-10-12 17:27:10

标签: c++

int split(string stringSplit, char charac, string arraySplit[], int arrsize)
{
   if (stringSplit.length() == 0) 
   {
       return 0;
   }
   string word = "";
   int counterArray = 0;
   stringSplit = stringSplit + charac;
   for (int i = 0; i < stringSplit.length(); i++)
   {
       if (stringSplit[i] == charac)
       {
           if (word.length() == 0)
           {
               continue;
           }
           arraySplit[counterArray] = word;
           counterArray++;
           word = "";
       }
       else 
       {
           word = word + stringSplit[i];
       }
   }
   return counterArray;
}

int readRatings (string fileName, string user[], int ratings [][50], int numUsers, int maxRows, int maxColumns)
{
    if (numUsers == maxRows)
    {
        return -2;
    }
    ifstream inputFile;
    inputFile.open (fileName);
    string line = "";
    if (inputFile.fail())
    {
        return -1;
    }
    string arr [maxColumns];
    int counterUser = 0;
    int row = 0;
    while (getline(inputFile, line))
    {
        if (line.length() > 0)
        {
            int count = split(line, ',', arr, maxColumns);
            user [counterUser] = arr [0];
            counterUser++;

        }
    }
    return counterUser;
}

这只是我代码的第一部分,不幸的是,我们只允许在c ++中使用您可以在此处看到的命令(给出或接受)。我似乎无法弄清楚为什么存在细分错误。我猜是因为它试图读取数组的空白部分,但我不知道如何更改它

0 个答案:

没有答案