繁体   English   中英

使用 C++ 读取带有两个连续分隔符的 csv 文件

[英]Read a csv file with two consecutive delimiters using C++

我正在尝试从 CSV 文件生成数独网格。 我为这种文件制作的代码:

1,2,3,4,5,6,7,8,9
1,2,3,4,5,6,7,8,9
...

但我需要它来使用这种文件创建网格:

; ; 5; ;;1; ;;1
3; ; ;;;;6; ;
;7;;;;2;;;4
;;1;3;;5;;8;
...

这是我的代码:

int grid[9][9];
    ifstream inputFile;
    inputFile.open(inputFileName);

    for (int row = 0; row <9 ; row++) {
        string line;
        getline(inputFile, line);
        if (!inputFile.good())
            break;
        stringstream ss(line);

        for (int col = 0; col < 9; col++) {
            size_t si = line.find_first_not_of("\n 123456789");
            string ch = line.substr(0, si); 
            if (ch.length() == 0) break; 
            istringstream converter(ch);
            converter >> grid[row][col];
            if ((si + 1u) >= line.length() || si == std::string::npos) break; 
            line = line.substr(si + 1u, std::string::npos); 
        }
    }


    for (int i = 0; i <= 8; i++) {
        for (int j = 0; j <= 8; j++) {
            if (grid[i][j] == 0) { 
                cout << "  "; 
            }
            else {
                cout << grid[i][j] << " "; 
            }
            if ((j + 1) % 3 == 0) {
                cout << "|"; 
            }
            if (j == 8)
                cout << "  " << (i + 1);
        }
        cout << endl;
        if ((i + 1) % 3 == 0) {
            for (int s = 0; s <= 20; s++)
                cout << "-";
            cout << endl;
        }
    }
    cout << endl;
    return 0;
}

你有什么想法用几个连续的分隔符来做吗?

非常感谢

for (int col = 0; col < 9; col++)的循环在if (ch.length() == 0) break; - 每个双冒号";;" 停止进一步阅读。 if ((si + 1u) >= line.length() || si == std::string::npos) break; 将匹配太早。

我将通过帮助程序 function 将每个输入行拆分为单个值。 这可能会使用更多的行,但更好地阅读和调试:

for (int row = 0; row < 9; row++) 
{
  string line;
  getline(inputFile, line);
  if (!inputFile.good())
  {
    break; // handle error 
  }
  size_t delimiterPos = line.find_first_not_of("\n 123456789");
  if (delimiterPos == std::string::npos)
  {
    break; // handle error
  }
  char delimiter = line[delimiterPos];
  vector<string> singleValues = split(clearBlanks(line), delimiter);
  if (singleValues.size() != 9)
  {
    break; // handle error 
  }
  for (size_t col = 0; col < singleValues.size(); col++)
  {
    grid[row][col] = singleValues[col].size() > 0 ? stoi(singleValues[col]) : 0;
  }
}

在两个添加的帮助下。 函数 - 从字符串中清除所有空格:

std::string clearBlanks(const std::string& input)
{
  std::string returnValue = input;
  size_t pos = returnValue.find(" ");
  while (pos != std::string::npos)
  {
    returnValue.erase(pos, 1);
    pos = returnValue.find(" ");
  }
  return returnValue;
} 

第二个将字符串拆分为字符串数组:

vector<string> split(const string& str, char delim)
{
  vector<string> tokens;
  size_t prev = 0, pos = 0;
  do
  {
    pos = str.find(delim, prev);
    if (pos == string::npos)
    {
      pos = str.length();
    }
    tokens.push_back(str.substr(prev, pos - prev));
    prev = pos + 1;
  } while (pos < str.length());
  return tokens;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM