簡體   English   中英

C-Strtok(),在'\\ n'上分割字符串,但保留定界符

[英]C - Strtok() , split the string on '\n' but keep the delimiter

我的C程序存在以下問題。 它的部分功能是讀取一些文本並將其拆分為句子,然后將這些句子寫入文件中。

我使用Strtok()將句子中的大部分文本分割(當\\ n出現時,句子結束),但是當句子中僅包含\\ n字符時,例如:

//////////////////////////////

您好,這是一些示例文本
這是第二句話

上面的句子只是換行
這是最后一句話。

/////////////////////////////

該文件的輸出如下:

0您好,這是一些示例文本
1這是第二句話
2上面的句子只是換行
3這是最后一句話。

///////////////////////////////////////////////////// //

雖然應該是:

0您好,這是一些示例文本
1這是第二句話
2
3上面的句子只是\\ n
4這是最后一句話。

////////////////////////////////////

包含字符串的文件應充當日志文件,這就是為什么我必須將文本塊拆分為\\ n拆分的句子,並且在將每個句子寫入文件之前將其前面帶有整數的原因。

這是與此功能相關的代碼:

int counter = 0; // Used for counting
const char s[2] = "\n"; // Used for tokenization

// ............

char *token;
      token = strtok(input,s);
      while(token != NULL){
        fprintf(logs, "%d ", counter);
        fprintf(logs, "%s\n" , token); // Add the new line character here since it is removed from the tokenization process
        counter++;
        token = strtok(NULL, s);
      }

// .........

當“空句子”(一個只是\\ n字符的句子)正確處理時,是否有一種特殊情況?

也許另一個函數可以代替strtok()工作?

如注釋所建議的那樣,您可能應該使用strstrstrchr ,但是如果由於某種原因您的應用程序需要strtok ,則可以節省每個句子結尾的位置,並確定多個換行符( \\n )是通過指針算術順序出現的。

未經測試的粗略示例代碼:

int counter = 0; // Used for counting
const char* last_sentence;


// ............
      last_sentence = input;
      char *token;
      token = strtok(input,"\n");
      while(token != NULL){
        int i;
        for (i = (token - last_sentence);i > 1; i--){
          // this gets called once for each empty line.
          fprintf(logs, "%d \n", counter++);
        }
        fprintf(logs, "%d %s\n", counter++, token);

        last_sentence = token + strlen(token);
        token = strtok(NULL, "\n");
      }

// .........

編輯:用strchr添加了示例

使用strchr也很容易,即使不是那么容易,尤其是因為只有一個定界符。 下面的代碼接受您的句子並將其拆分。 它只是打印它們,但是您可以根據自己的目的輕松擴展它。

#include <stdio.h>
#include <string.h>
const char* sentences = "Hello, this is some sample text\n"
                        "This is the second sentence\n"
                        "\n"
                        "The sentence above is just a new line\n"
                        "This is the last sentence.\n";

void parse(const char* input){
  char *start, *end;
  unsigned count = 0;

  // the cast to (char*) is because i'm going to change the pointer, not because i'm going to change the value.
  start = end = (char*) input; 

  while( (end = strchr(start, '\n')) ){
      printf("%d %.*s", count++, (int)(end - start + 1), start);
      start = end + 1;
  }
}

int main(void){
  parse(sentences);
}

如果要從文件讀取輸入,則可以使用流(帶有fopen() )和getline()

另外,您可以編寫一個計算\\n數量,分配char*數組並逐行填充的函數。

編輯:如果您不想自己編寫代碼,則可以通過一些小型研究輕松找到它

您已將換行符\\n包含在strtok的定界符集中。

如果輸入字符串是有效的讀取,並且對strtok的第一次調用返回NULL ,則它是空白行,您可以對其進行處理。

token = strtok(input,s);
if(token == NULL) {
    fprintf(logs, "%d\n", counter);
    counter++;
}
while(token != NULL){                   // the `while` serves as `else`
    fprintf(logs, "%d ", counter);
    fprintf(logs, "%s\n" , token);
    counter++;
    token = strtok(NULL, s);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM