如何解决C++ 写入字符串流无法正常工作
这些天我一直在为 CHIP-8 制作汇编程序,今天我尝试实现操作码的参数以使其正常工作。但是,当我必须编写 2 个参数来完成操作码时,我有以下代码:
// Chip8Instruction is a struct I've made that holds the final machine code to be written,the mnemonic,// the argument count and the position for each argument in hex
void writeTwoArguments(Chip8Instruction instruction,const std::string& line,int lineNum,const std::string& romName) {
unsigned int arg1;
unsigned int arg2;
std::string remainingLine = line;
std::string testStr;
std::stringstream stringStream;
// Arguments are comma and then space separated,assume I give 3,FF
std::string::size_type commaPos = line.find(',');
if (commaPos != std::string::npos) {
stringStream << std::hex << remainingLine.substr(0,commaPos); // This writes 3,like it should
testStr = stringStream.str(); // Holds "3",like it should
stringStream >> arg1; // This holds 0x3,like it should
stringStream.str("");
remainingLine.erase(0,commaPos+2); // FF remains,like it should
stringStream << std::hex << remainingLine;
testStr = stringStream.str(); // This holds nothing but it should have "FF",if I don't empty the stream it holds "3" from before
stringStream >> arg2; // This also holds nothing but it should have 0xFF,holds 0x3 if not empty stream
instruction.machineCode = instruction.start + (arg1 * instruction.arg1Pos) + (arg2 * instruction.arg2Pos);
}
writeMultipleDigitsToROM(romName,instruction.machineCode,lineNum);
}
最小可重现示例:
#include <iostream>
#include <sstream>
int main() {
std::string line = "3,FF";
std::stringstream stringStream;
unsigned int int1;
unsigned int int2;
// Get position of comma
std::string::size_type commaPos = line.find(',');
// Get everything up to the comma
stringStream << std::hex << line.substr(0,commaPos);
stringStream >> int1; // This holds 0x3,like it should
stringStream.str("");
line.erase(0,commaPos+2); // "FF" remains,like it should
stringStream << std::hex << line;
stringStream >> int2; // This is empty but should be 0xFF
}
正如代码注释所描述的,保存的值要么是错误的,要么是过时的。可能是什么问题、原因以及如何解决?
感谢所有帮助,谢谢!
解决方法
我尝试使用此代码隔离问题:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string input = "3,FF";
stringstream ss;
ss << input.substr(0,1);
cout << ss.str() << endl;
input.erase(0,3);
ss.str("");
cout << input << endl;
ss << std::hex << input;
cout << ss.str() << endl;
return 0;
}
但是,它似乎在我这边工作正常。不知道有什么区别,抱歉,除非我在输入格式中犯了错误。顺便说一句,我建议,不要提取到字符串流,而是通过将输入包装在字符串流中来提取它,如下所示:
int arg1,arg2;
string comma;
stringstream ss(input);
ss >> std::hex >> arg1 >> comma >> arg2;
cout << arg1 << endl;
cout << arg2 << endl;
更好的是,您可以为您的类型重载流提取运算符 operator>>
,因此它可能是这样的:
Chip8Instruction c;
ss >> c;
// do stuff now that c is filled in
您可以通过声明以下内容来实现:
istream& operator>>(istream& in,Chip8Instruction& out) {
// where your code to parse the string could go
}
抱歉,我无法解决实际的解析问题,但我不清楚我所做的与您的不同之处。
,第一:std::hex
改变了整数打印或扫描的方式。它对 line
没有影响 - line
是一个字符串。 stringStream << std::hex << line;
与 stringStream << line;
相同。
执行 stringStream >> int1;
后,eof
位在 stringStream
内设置。要扫描其他任何内容,您必须先清除它。请研究如何重置 stringstream
。
要读取 hex
数字,您必须告诉您的流您要读取一个十六进制数字。 F
不是数字!
但总的来说,用法很奇怪 - 只需从 stringstream
读取内容,它实际上是要从中读取数据的东西。忘记您的 string
- 在 stream
上工作。记住处理错误。我可以看到:
#include <iostream>
#include <sstream>
#include <iomanip>
int main() {
std::string line = "3,FF";
std::istringstream ss(line);
unsigned int int1;
unsigned int int2;
if (!(
(ss >> std::dec >> int1) &&
ss.get() == ',' &&
(ss >> std::hex >> int2)
)) {
std::cerr << "read failed\n";
abort();
}
std::cout << int1 << " " << int2 << "\n";
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。