从fputs拆分字符串

时间:2014-08-31 16:09:26

标签: c split strtok fputs

#include <stdio.h>

int main()
{
   FILE *fp;
   char str[60];
   char data[50];
   char * pch;

   /* opening file for reading */
   fp = fopen("DATAtest.txt" , "r");
   if(fp == NULL) {
      perror("Error opening file");
      return(-1);
   }
   if( fgets (str, 60, fp)!=NULL ) {
      /* writing content to stdout */
      //puts(str);
   }
if( fgets (str, 60, fp)!=NULL ) {
      /* writing content to stdout */
      puts(str);

printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
    {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
      }
       fclose(fp);
       return(0);
    }

基本上它的作用是打开文件并从第二行提取数据。接下来应该做什么(从行:printf(&#34; Splitting ...)),是将获得的文本拆分为单独的字符。例如:我得到以下文字&#34; 0 0 128 0 0 0 0 0 0 0;我想这样拆分:

0 
0
128
0
0
0
0
0
0
0

对于我刚开始使用的代码感到抱歉。

3 个答案:

答案 0 :(得分:0)

您能否告诉我您的文件内容。我用for循环替换了你的while循环,因为它更简洁,更容易理解:

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

int main()
{
    FILE *fp;
    char str[60];
    char data[50];
    char * pch;

    /* opening file for reading */
    fp = fopen("DATAtest.txt" , "r");
    if(fp == NULL) {
        perror("Error opening file");
        return(-1);
    }
     if( fgets (str, 60, fp)!=NULL ) {
      /* writing content to stdout */
      //puts(str);
     }

    if( fgets (str, 60, fp)!=NULL ) {
        /* writing content to stdout */
        puts(str);

        printf ("Splitting string \"%s\" into tokens:\n",str);
        for(pch = strtok(str, " ,.-"); pch != NULL; pch = strtok(NULL, " ,.-"))
        {
            puts(pch);
        }
        fclose(fp);
    }
    return 0;
}

答案 1 :(得分:0)

我认为这是DATAtest.txt文件的样子:

abcd pqr strm
" 0 0 128 0 0 0 0 0 0 0;
xyz abr

我对您的代码做了一些小改动:

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

int main()
{
   FILE *fp;
   char str[60];
   char data[50];
   char * pch;

   /* opening file for reading */
   fp = fopen("DATAtest.txt" , "r");
   if(fp == NULL) {
      perror("Error opening file");
      return(-1);
   }
   if( fgets (str, 60, fp)!=NULL ) {
      /* writing content to stdout */
      //puts(str);
   }
   if( fgets (str, 60, fp)!=NULL ) 
   {
        /* writing content to stdout */
        puts(str);
        str[strlen(str)-1]='\0'; // Remove \n from str
        printf ("Splitting string \"%s\" into tokens:\n",str);
        pch = strtok (str,"\" ,.-;"); //Mention required delimiters 
        while (pch != NULL)
        {
            printf ("%s\n",pch);
            pch = strtok (NULL, "\" ,.-;");//Mention required delimiters
        }
    }
    fclose(fp);
    return(0);
}

<强>输出

" 0 0 128 0 0 0 0 0 0 0;

Splitting string "" 0 0 128 0 0 0 0 0 0 0;" into tokens:
0
0
128
0
0
0
0
0
0
0

答案 2 :(得分:0)

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

 int main(){
     FILE *fp;
     char str[60] = " 0 0 128 0 0 0 0 0 0 0;";//from fgets
     char *data[50];//or char data[max number of parts][max of lengh of parts]
     char *pch;
     const char *delimiter = " ,.-;";
     int i, cnt = 0;

     printf ("Splitting string \"%s\" into tokens:\n",str);

     pch = strtok (str, delimiter);
     while (pch != NULL){
         //printf ("%s\n", pch);
         //strcpy(data[cnt++], pch);//by 2D array, Not necessary free.
         data[cnt++] = strdup(pch);//make clone. it's not standard function.
         pch = strtok (NULL, delimiter);
     }
     for(i = 0; i<cnt; ++i){
         printf("%s\n", data[i]);
         free(data[i]);//release
     }
     return(0);
 }