2014-08-31 2 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; 이 방법으로 분할하고 싶습니다.fputs에서 문자열 분할

0 
0 
128 
0 
0 
0 
0 
0 
0 
0 

코드에 대해 죄송합니다. 방금 시작했습니다.

+0

두 개의'fgets' 호출이 있습니다. 첫 번째 호출의 목적은 무엇입니까 (파일의 첫 번째 줄을 버릴 가능성이 높음)? – cnicutar

+0

이것은 DATAtest.txt의 처음 두 줄에 60 자 미만이 있다고 가정합니다. (첫 줄이 60 자보다 길면 첫 번째 59 자만 움켜 잡습니다.) –

+0

프로그램에 (#include 이 없어도) 주어진 입력으로 원하는 것을 수행합니다. 무엇이 문제입니까? – Armali

답변

0

파일 내용을 알려주십시오.

#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

어디에 있나요? 그의 두 번째 라인은? – Igor

+0

@JayElston 나는 그 라인을 돌려 줬다. – Igor

0

을 나는이처럼 DATAtest.txt 파일이 모습입니다 가정 : 나는 사소한 변경 한

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

은 이해하기가 더 간결하고 쉽게 때문에 for 루프와 while 루프를 교체 코드 :

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