尝试使用超载操作员读取二进制文件时进行分割故障>>
我正在尝试读取使用这样的代码创建的二进制文件:
#include <list>
#include <string>
#include <bitset>
#include <fstream>
struct Node {
char data;
int frequency;
friend std::istream& operator>>(std::istream& input, Node& e) {
input.read(&e.data, sizeof(e.data));
input.read(reinterpret_cast<char*>(e.frequency), sizeof(e.frequency));
return input;
};
friend std::ostream& operator<<(std::ostream& output, const Node& e) {
output.write(&e.data, sizeof(e.data));
output.write(reinterpret_cast<const char*>(&e.frequency), sizeof(e.frequency));
return output;
};
};
int main() {
std::list<struct Node> list;
for(char i='a'; i<='z'; i++) {
struct Node n;
n.data = i;
n.frequency = 1;
list.push_back(n);
}
std::string encoded_file = "";
for(int i=0; i<100; i++) {
encoded_file = encoded_file + "101010";
}
std::fstream output;
output.open("output.txt", std::ios_base::out | std::ios_base::binary);
if (output.is_open()) {
output << list.size();
for(int i=0; i<list.size(); i++) {
output << list.front();
list.pop_front();
}
for(long unsigned int i=0; i<encoded_file.length(); i+=8) {
std::string data = encoded_file.substr(i, 8);
std::bitset<8> b(data);
unsigned long x = b.to_ulong();
unsigned char c = static_cast<unsigned char>( x );
output << c;
}
}
output.close();
return 0;
}
此代码似乎可以正常工作,并且文件outpt.txt
是没有问题的。
但是,当我尝试使用此代码读取文件时:
#include <list>
#include <string>
#include <bitset>
#include <fstream>
#include <iostream>
struct Node {
char data;
int frequency;
friend std::istream& operator>>(std::istream& input, Node& e) {
input.read(&e.data, sizeof(e.data));
input.read(reinterpret_cast<char*>(e.frequency), sizeof(e.frequency));
return input;
};
friend std::ostream& operator<<(std::ostream& output, const Node& e) {
output.write(&e.data, sizeof(e.data));
output.write(reinterpret_cast<const char*>(&e.frequency), sizeof(e.frequency));
return output;
};
};
int main() {
std::list<struct Node> list;
std::string encoded_file = "";
std::fstream input;
input.open("output.txt", std::ios_base::in | std::ios_base::binary);
if (input.is_open()) {
std::cout << "1" << std::endl;
int size = 0;
input >> size;
std::cout << "size: " << size << std::endl;
for(int i=0; i<size; i++) {
Node node;
input >> node;
std::cout << node.data << " (" << node.frequency << ")" << std::endl;
list.push_back(node);
}
std::cout << "2" << std::endl;
char c;
while(input.get(c)) {
std::bitset<8> b(c);
encoded_file = encoded_file + b.to_string();
}
std::cout << "3" << std::endl;
}
input.close();
return 0;
}
我会得到一个细分错误。当我尝试执行输入&gt;&gt;节点;
。我检查了一下,显然,当程序输入node :: operator&gt;
,e.data
已读取,但频率不是。
谁能给我任何解决此问题的提示?
I am trying read a binary file which was created with code like that:
#include <list>
#include <string>
#include <bitset>
#include <fstream>
struct Node {
char data;
int frequency;
friend std::istream& operator>>(std::istream& input, Node& e) {
input.read(&e.data, sizeof(e.data));
input.read(reinterpret_cast<char*>(e.frequency), sizeof(e.frequency));
return input;
};
friend std::ostream& operator<<(std::ostream& output, const Node& e) {
output.write(&e.data, sizeof(e.data));
output.write(reinterpret_cast<const char*>(&e.frequency), sizeof(e.frequency));
return output;
};
};
int main() {
std::list<struct Node> list;
for(char i='a'; i<='z'; i++) {
struct Node n;
n.data = i;
n.frequency = 1;
list.push_back(n);
}
std::string encoded_file = "";
for(int i=0; i<100; i++) {
encoded_file = encoded_file + "101010";
}
std::fstream output;
output.open("output.txt", std::ios_base::out | std::ios_base::binary);
if (output.is_open()) {
output << list.size();
for(int i=0; i<list.size(); i++) {
output << list.front();
list.pop_front();
}
for(long unsigned int i=0; i<encoded_file.length(); i+=8) {
std::string data = encoded_file.substr(i, 8);
std::bitset<8> b(data);
unsigned long x = b.to_ulong();
unsigned char c = static_cast<unsigned char>( x );
output << c;
}
}
output.close();
return 0;
}
This code seems to work fine, and the file output.txt
is generated without problems.
But, when I try reading the file with this code:
#include <list>
#include <string>
#include <bitset>
#include <fstream>
#include <iostream>
struct Node {
char data;
int frequency;
friend std::istream& operator>>(std::istream& input, Node& e) {
input.read(&e.data, sizeof(e.data));
input.read(reinterpret_cast<char*>(e.frequency), sizeof(e.frequency));
return input;
};
friend std::ostream& operator<<(std::ostream& output, const Node& e) {
output.write(&e.data, sizeof(e.data));
output.write(reinterpret_cast<const char*>(&e.frequency), sizeof(e.frequency));
return output;
};
};
int main() {
std::list<struct Node> list;
std::string encoded_file = "";
std::fstream input;
input.open("output.txt", std::ios_base::in | std::ios_base::binary);
if (input.is_open()) {
std::cout << "1" << std::endl;
int size = 0;
input >> size;
std::cout << "size: " << size << std::endl;
for(int i=0; i<size; i++) {
Node node;
input >> node;
std::cout << node.data << " (" << node.frequency << ")" << std::endl;
list.push_back(node);
}
std::cout << "2" << std::endl;
char c;
while(input.get(c)) {
std::bitset<8> b(c);
encoded_file = encoded_file + b.to_string();
}
std::cout << "3" << std::endl;
}
input.close();
return 0;
}
I get a segmentation fault. The error occurs when I try to execute input >> node;
. I checked, and apparently when the program enters Node::operator>>
, e.data
is read, but frequency is not.
Can anyone give me any tips of how to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
node ::操作员&gt;
中有一个错字。阅读e.frequency
时,您缺少&amp;
:这是我的上一个答案您从哪里获得此代码。我已经纠正了这个错误。
话虽如此,我在您的代码中看到了其他问题。
您正在以二进制模式创建文件,但是您并未将所有内容作为二进制值流式传输。只有您的
节点
类作为二进制流进行流,但是您的其他值是作为文本流式传输的。不要混合格式方案。输出&lt;&lt; list.size()
和output&lt;&lt; C
正在执行格式I/O的格式。使用write()
而不是operator&lt;&lt;
用于二进制i/o,例如:然后在读取文件时反转该过程,例如:
在代码中,在您的代码中正在创建文件,您的第一个
循环是错误的。您正在修改
list
时,因此会影响其size()
。因此,您最终会跳过列表中的节点。您应该使用迭代器而不是索引迭代,并且根本不修改列表,例如:或更简单:
You have a typo in your
Node::operator>>
. When readinge.frequency
, you are missing a&
:That was a typo in my previous answer where you got this code from. I have corrected that mistake.
With that said, I see other problems in your code.
You are creating the file in binary mode, but you are not streaming everything into it as binary values. Only your
Node
class is being streamed as binary, but your other values are being streamed as text instead. Don't mix formatting schemes.output << list.size()
andoutput << c
are performing formatted I/O. Usewrite()
instead ofoperator<<
for binary I/O, eg:And then reverse that process when reading the file, eg:
Also, in your code that is creating the file, your 1st
for
loop is wrong. You are modifying thelist
while looping through it, thus affecting itssize()
. So you end up skipping nodes in the list. You should iterate using iterators instead of indexes, and without modifying the list at all, eg:Or simpler: