问题描述
如标题所示,如何使用 fstream
?
As the title says, how do you read hex values using fstream
?
(假设文件中有FF。)
i have this code: (let's say we have "FF" in the file.)
fstream infile;
infile.open(filename, fstream::in|fstream::out|fstream::app);
int a;
infile >> std::hex;
infile >> a;
cout << hex << a;
但这不会给我任何输出,而不是 ff
。我知道有一个 fscanf(fp,%x,val)
但我很好奇有没有任何方法这样做使用流库。
but this does not give me any output instead of ff
. I know there is a fscanf(fp, "%x", val)
but I am curious is there any way to do this using stream library.
UPDATE :
我的代码一直运行正常,原来我的错误是我不能读取FFF
并将其放入变量a,b,c中
My code was right all along, it turns out my error was I couldn't read "FFF"
and put it in variable a,b,c like this
while (infile >> hex >> a >> b >> c)
{
cout << hex << a << b << c << "\n";
}
有人可以帮我吗?我必须分离每个HEX值我想用空间阅读吗?
因为 infile>> hex>> setw(1)
不起作用。
Can somebody help me with this? do I have to separate every HEX values i want to read with space?because infile >> hex >> setw(1)
doesn't work..
推荐答案
p>
You can use the hex modifier
int n;
cin >> hex >> n;
这篇关于如何使用fstream中的fstream从文件中读取十六进制值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!