使用fscanf时,宽度为变量 [英] Width as a variable when using fscanf
本文介绍了使用fscanf时,宽度为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想读取文件的某一部分,每行的数据量不同,但我知道我想要多少字节的信息。像这样:
5bytes.byte1byte2byte3byte4byte5CKSum //其中每行的字节数不同(并且没有周期只有可读性)
实际数据:
05AABBCCDDEE11
03AABBCC22
04AABBCCDD33
以使我的宽度成为这样的变量:
fscanf_s(in_file,%variableX,& iData);
这是可能的,因为现在我想我必须创建一个case语句吗?不幸的是,没有对printf使用'*'这样的修饰符,这会导致scanf从变量中获取它的字段宽度或精度。您可以到达的最接近的是动态创建格式字符串:
char格式[8]
sprintf(format,%%% dX,width);
fscanf(in_file,format,& iData);
I am trying to read in a certain portion of a file and that amount of data is different per line but I know how how many bytes of info I want. Like this:
5bytes.byte1byte2byte3byte4byte5CKSum //where # of bytes varies for each line (and there is no period only there for readability)
Actual data:
05AABBCCDDEE11
03AABBCC22
04AABBCCDD33
So I want to have my width be a variable like this:
fscanf_s(in_file,"%variableX", &iData);
Is this possible, because right now I'm thinking I have to create a case statement?
解决方案
Unfortunately, no, there's no modifier like '*' for printf that causes scanf to get its field width or precision from a variable. The closest you can come is dynamically creating the format string:
char format[8];
sprintf(format, "%%%dX", width);
fscanf(in_file, format, &iData);
这篇关于使用fscanf时,宽度为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文