fscanf_s()使用

测试文件data.txt

1.0,3.14159

main.cpp

FILE *f = NULL;
int ret = fopen_s(&f, "./data.txt", "r");
if(ret) {
	return ret;
}
double val1 = 0, val2 = 0;
ret = fscanf_s(f, "%lf,%lf", &val1, &val2); // correct
// ret = fcanf_s(f, %lf,%lf", &val1, &val2, sizeof(double)); // correct
// ret = fcanf_s(f, %lf,%lf", &val1, sizeof(double), &val2, sizeof(double)); // false
if(ret) {
	fclose(f);
	return ret;
}
printf("val1: %lf, val2: %lf\n", val1, val2);
fclose(r)

对于fscanf_s()来说,只设置一个长度参数或者不指定长度参数,都可以通过,为每个数据都指定反而是错的,fscanf_s()不如fscanf()函数来的直接。

你可能感兴趣的:(C程序设计语言,基础,c++)