简体   繁体   中英

read string from file and turn into 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?

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
    }
}

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>

EDIT:

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)); } 

EDIT : Credit to @PeterT

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()]); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM