只要文件是这种格式,此代码就会读取文件并打印它
word,12,34
words,40,20
another,20,11
如果它之间有这样的空行,我该如何让它做同样的事情,因为现在它只是给我一个分段错误
word,12,34
words,40,20
another,20,11
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
FILE *pdata;
char buffer[1000];
char *token, *del=",";
int value1;
double value2;
pdata = fopen ("file.data", "r");
if(pdata == NULL)
{
printf("unable to open file \n");
exit(1);
}
while( fgets(buffer, sizeof(buffer), pdata) != NULL )
{
token = strtok(buffer, del);
value1 = atoi( strtok(NULL, del) );
value2 = atof( strtok(NULL, del) );
printf("%s is %.3lf\n", token, value1 + value2);
}
fclose( pdata );
return 0;
}
请帮忙