#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
對不起,我剛剛開始這段代碼。
你有兩個'fgets'調用。第一次通話的目的是什麼(可能會丟棄文件的第一行)? – cnicutar 2014-08-31 16:13:53
這假設DATAtest.txt中的前兩行少於60個字符。 (如果第一行的長度超過60個字符,那麼只會抓取前59個字符。 – 2014-08-31 16:21:12
你的程序(雖然缺少'#include')對給定的輸入做了你想要的 - 那麼問題是什麼? –
Armali
2015-05-08 09:20:19