2014-08-31 70 views
0
#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(「Splitting ...」)),是將獲得的文本拆分爲單獨的字符。例如:我得到以下文本「0 0 128 0 0 0 0 0 0 0;我想要這樣拆分:從fputs分割一個字符串

0 
0 
128 
0 
0 
0 
0 
0 
0 
0 

對不起,我剛剛開始這段代碼。

+0

你有兩個'fgets'調用。第一次通話的目的是什麼(可能會丟棄文件的第一行)? – cnicutar 2014-08-31 16:13:53

+0

這假設DATAtest.txt中的前兩行少於60個字符。 (如果第一行的長度超過60個字符,那麼只會抓取前59個字符。 – 2014-08-31 16:21:12

+0

你的程序(雖然缺少'#include ')對給定的輸入做了你想要的 - 那麼問題是什麼? – Armali 2015-05-08 09:20:19

回答

0

你能告訴我你的文件的內容。我換成一個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; 
} 
+0

Where is他的第二行? – Igor 2014-08-31 16:25:09

+0

@JayElston我返回了那一行。 – Igor 2014-08-31 16:48:22

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 
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); 
}