Using fscanf to read comma delimited doubles

帅比萌擦擦* 提交于 2020-01-24 01:24:29

问题


How would I read comma delimited doubles with no white space?

I tried the following:fscanf(file, "%lf[^,], &x) but it doesn't work.

The file will be in the following format:

1.0,2.0,4.0
3.0,6.0,1.0

回答1:


Instead of using [^,] regular expression you directly use , .

#include <stdio.h>  
int main(){  
FILE *fp;  
double buff[255];
int i=0;
fp = fopen("file.txt", "r");  
while(fscanf(fp, "%lf,",&buff[i++])!=EOF){  
printf("%0.1lf ", buff[i-1] );  
}  
fclose(fp);  
}  


来源:https://stackoverflow.com/questions/46761656/using-fscanf-to-read-comma-delimited-doubles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!