从文件中读取字符串,并转换为bitset 12. [英] read string from file and turn into bitset<12>
问题描述
我正在尝试从txt文件中读取字符串,并将其转换为二进制格式的bitset< 12>字符串文件.
Hi I'm trying to read string from txt file and transform it into binary file which is bitset<12> string form.
int main()
{
using namespace std;
std::ifstream f("fruit.txt");
std::ofstream out("result.txt");
std::hash<std::string>hash_fn;
int words_in_file = 0;
std::string str;
while (f >> str){
++words_in_file;
std::bitset<12>* bin_str = new std::bitset<12>[3000];
int temp_hash[sizeof(f)];
std::size_t str_hash = hash_fn(str);
temp_hash[words_in_file] = (unsigned int)str_hash;
bin_str[words_in_file] = std::bitset<12>((unsigned int)temp_hash[words_in_file]);
out << bin_str[words_in_file] << endl;
delete[] bin_str;
}
out.close();
}
但是有错误.我该如何更改?
but there is error. How can I change it?
推荐答案
这是我编写的一些代码,可将输入文件"file.txt"
转换为二进制文件.它通过获取每个字符的ascii值并将该数字表示为二进制值来实现,尽管我不确定如何在此处将bin_str
写入文件.
Here is some code that I wrote that turns the input file "file.txt"
into binary. It does this by taking the ascii value of each character and representing that number as a binary value, although I'm not sure how to write bin_str
to a file here.
#include <string>
#include <fstream>
#include <streambuf>
#include <bitset>
#include <iostream>
int main(){
std::ifstream f("file.txt");
std::string str((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>()); // Load the file into the string
std::bitset<12> bin_str[str.size()]; // Create an array of std::bitset that is the size of the string
for (int i = 0; i < str.size(); i++) {
bin_str[i] = std::bitset<12>((int) str[i]); // load the array
std::cout << bin_str[i] << std::endl; // print for checking
}
}
侧面说明:
std::bitset<12>
可能不是您想要的,如果您查看ascii字符,您可以拥有的最大数字是127,而二进制只有7位数字,所以我假设您想要的像是std::bitset<7>
或std::bitset<8>
SIDE NOTE:
std::bitset<12>
may not be what you want, if you look at ascii characters the highest number you can have is 127 and in binary that's only 7 digits so I'd assume you'd want something more like std::bitset<7>
or std::bitset<8>
如果要将其写入文件,则需要使用std::ios::binary
打开文件,然后循环遍历位集数组,并将其从to_ulong()
给定的无符号长表示形式写为const char指针. ((const char*)&ulong_bin
).现在,当您使用二进制编辑器打开文件时,您会看到二进制写入和常规写入之间的区别,但是您会注意到,像cat
这样的程序仍可以将您编写的二进制文件解密为简单的ascii字母./p>
If you want to write it to a file you'll need to open a file with std::ios::binary
and then loop through the array of bitsets and write their unsigned long representative, given from to_ulong()
, as a const char pointer ((const char*)&ulong_bin
). Now when you open the file with a binary editor you will see the difference between the binary write and the regular write, but you'll notice that programs like cat
can still decipher the binary you've written as simple ascii letters.
std::ofstream out("file.bin", std::ios::binary);
for (int i = 0; i < str.size(); i++) {
unsigned long ulong_bin = bin_str[i].to_ulong();
out.write((const char*)&ulong_bin, sizeof(ulong_bin));
}
编辑:归功于@PeterT
EDIT: Credit to @PeterT
引起我注意的是,C ++ 11及更高版本不支持VLA(可变长度数组),因此std::bitset<12> bin_str[str.size()];
行应更改为以下内容之一:
It has come to my attention that VLAs, variable length arrays, are not supported in C++11 and up so the line std::bitset<12> bin_str[str.size()];
should be changed to one of the following:
std::bitset<12> *bin_str = new std::bitset<12>[str.size()]; // make sure you delete it later
// OR
std::vector<std::bitset<12>> bin_str(str.size());
// OR
std::unique_ptr<std::bitset<12>[]> str_ptr (new std::bitset<12>[str.size()]);
这篇关于从文件中读取字符串,并转换为bitset 12.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!