【问题标题】:remove whitespaces tail from string (char*)从字符串中删除空格尾部(char *)
【发布时间】:2012-09-16 23:17:58
【问题描述】:

你好,我有行格式的文本文件

1|few ewf ew fewfew 

我需要解析它,但我不知道如何从我不知道的第二个值中删除空格尾部。

这是我目前为止的segmentation fault尝试

char* token1;
char* token2;
char* search = "|";
char* search2 = "  "; // double space because string can contains single space
while (fgets(line, 150, f)) {
  token1 = strtok(line, search);
  token2 = strtok(search, search2); // <- segfault
  //token2 = strtok(NULL, search); <- contains a lot of spaces in tail

【问题讨论】:

  • 使用rtrim()?
  • strtok 修改作为第一个参数传递的字符串。您的变量search 指向一个无法修改的常量字符串。因此你得到一个分段错误
  • @Crozin 请提供它作为答案)

标签: c string file removing-whitespace


【解决方案1】:

看看deblank 函数。这是一个链接:http://www.mathworks.com/help/matlab/ref/deblank.html

【讨论】:

    猜你喜欢
    • 2023-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多