我正在尝试读取文件的某个部分,每行的数据量不同,但我知道我想要多少字节的信息。像这样:
5bytes.byte1byte2byte3byte4byte5CKSum //where # of bytes varies for each line (and there is no period only there for readability)
实际数据:
05AABBCCDDEE11
03AABBCC22
04AABBCCDD33
所以我想让我的宽度是这样的变量:
fscanf_s(in_file,"%variableX", &iData);
这可能吗,因为现在我想我必须创建一个案例陈述?
最佳答案
不幸的是,不,没有像'*'这样的修饰符用于 printf 导致 scanf 从变量中获取其字段宽度或精度。最接近的方法是动态创建格式字符串:
char format[8];
sprintf(format, "%%%dX", width);
fscanf(in_file, format, &iData);
关于c++ - 使用 fscanf 时将宽度作为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3313299/