#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; 我想这样拆分:
0
0
128
0
0
0
0
0
0
0
对不起,我刚开始的代码。