2015-10-05 71 views
0

在C中,我正在做一個標記器。我就麻煩了,因爲我發現,如果我鍵入:printf刪除內容 r在C

printf ("String of text\r"); 

所有被'\r'之前所著不打印。

所以,如果我要來標記"String of text\r",最後令牌應該"Text",不,它" ext"

有人知道爲什麼嗎?

編輯:此代碼。如果我打印ARGS [0],ARGS [1] ...和Im炭[] STR = 「卡德納德texto \ R」

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

     int 
     detectpattern(char target) 
     { 
      int patternsize; 
      int contador; 
      int found; 
      char pattern[] = {'\t', '\r', ' ', '\n', ' '}; 

      patternsize = sizeof(pattern)/sizeof(char); 
      contador = 1; 

      while (contador <= patternsize) { 
       if (pattern[contador] == target) { 
        found = 1; 
        break; 
       } 
       else { 
        found = 0; 
       } 
       contador++; 
       } 
      return found; 
     } 

     void 
     ispattern(char *str, int *inword){ 
      if (*inword != 0) { 
       *inword = 0; 
       *str = '\0'; 
      } 
     } 

     void 
     isword(char *str, int *inword, char **args, int *words){ 
      if (*inword == 0) { 
       *inword = 1; 
       args[*words] = str; 
       *words = *words + 1; 
      } 
     } 

     int 
     mytokenize(char *str, char **args, int maxargs) { 
      int i; 
      int intword; 
      int intwords; 
      int * inword = &intword; 
      int * words = &intwords; 

      intword = 0; 
      intwords = 0; 
      i = 0; 

      if (!str[i]) { 
       printf("String no válido"); 
       exit(0); 
      } 

      while (str[i] != '\0') { 
       if (detectpattern(str[i])) { 
        ispattern(&str[i], inword); 
       } else { 
        isword(&str[i], inword, args, words); 

       } 
       if (*words == maxargs) { 
       break; 
       } 
       i++; 
      } 
      return *words; 
     } 

     enum{ 
      maxargs = 2, 
     }; 

     int 
     main(int argc, char *argv[]){ 
      char str[] = "cadena de texto"; 
      char *strptr = &str[0]; 
      char *array_punteros[maxargs]; 

      mytokenize(strptr, array_punteros, maxargs); 

      exit(0); 
     } 
+0

請發佈標記器代碼。 –

+0

這似乎是2個不同的問題?你想知道打印字符串還是標記字符串? –

+0

另外,'\ r'是回車,也許看看[this](http://stackoverflow.com/questions/9253250/need-help-understanding-how-nb-and-rill-render- printf-output) –

回答

0

\r是Cariage返回。它使您的終端仿真器將光標移動到行的開頭 - 甚至可能會擦除行。嘗試打印到文件並使用vi或emacs等編輯器打開它。

下面是一個例子:

#include <stdio.h> 
int main(int argc, char **argv) { 
    printf("aaa test\r"); 
    printf("bbb test\r"); 

} 

產生只是bbb test在終端上。

測試與cygwin的GCC 4.9。

+1

它不會擦除線條。 –

+1

是的,沒有打印,因爲沒有''n''並且沒有'fflush()'。 –

+0

'fflush'沒有幫助,出於某種原因。但'printf(「文本字符串\ r \ n」);'工作,這意味着'\ r'不會擦除行。 –