使用fscanf读取逗号分隔的双打 [英] Using fscanf to read comma delimited doubles

查看:128
本文介绍了使用fscanf读取逗号分隔的双打的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何读取没有空格的逗号分隔的双打?

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

我尝试了以下操作: fscanf(file,%lf [^ ,],& x),但它不起作用。

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

文件将采用以下格式:

1.0,2.0,4.0
3.0,6.0,1.0


推荐答案

直接使用<$ c而不是使用 [^,] 正则表达式$ c>,。

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

这篇关于使用fscanf读取逗号分隔的双打的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆