将文本添加到行尾

时间:2012-09-13 13:06:14

标签: c file-io split

我正在处理一个文件。我想拍摄每一行并在空格后分开。 例如,如果line1有:今天是星期一,我希望今天有,星期一分开,以便对他们工作

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

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 个答案:

答案 0 :(得分:-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;
}