0

我正在处理一个文件。我想取每一行并在空格后分割。例如,如果 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 );

            }
4

1 回答 1

-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;
}
于 2012-09-13T13:08:11.517 回答