2012-09-13 57 views
0

我正在处理文件。我想把每一行都分开,并在空格后分开。 例如,如果一号线有:今天是星期一我希望能有今天,是,周一separatly为了对他们的工作将文本添加到行尾

这是到目前为止我的代码:

FILE *file = fopen ("file1", "r"); 

      if ((file != NULL)) 
      { 
       char line [ 128 ]; 
       while((fgets (line, sizeof line, file) != NULL)) 
       { 
       //tokenize the line based on space 

       ?? 

       } 
how to add text at the end of the line? i mean i have **today is monday** and i want to add for example **Yupppy** at the end of today is monday line. 

      fclose (file); 

      } 
+1

你可以使用strtok – Vijay

+0

可以请你发表一个例子吗?thx –

+0

我还需要在每行的结尾添加一个新的工作。你可以帮我吗?thx –

回答

-1

来标记一个字符串即可使用strtok() 检查我的链接后的例子,只是改变分隔空间:

(从链接的示例代码)

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




int main() 
{ 
  char str[] ="- This, a sample string."; 
  char * pch; 
  printf ("Splitting string \"%s\" into tokens:\n",str); 
  pch = strtok (str," "); 
  while (pch != NULL) 
  { 
    printf ("%s\n",pch); 
    pch = strtok (NULL, " "); 
  } 
  return 0; 
} 
+0

当然,应该注意的是'strtok'不是可重入的,应该使用'strtok_r'而不是可用的(甚至是'strsep',我认为它更清洁)。 –

+0

我正在c。谢谢。以及如何在char []的末尾添加单词? –

+0

你能帮忙吗? –