How to convert a .txt file of 0's and 1's to a .bin?

1 day ago 3
ARTICLE AD BOX

I am working on a project in which the first part I am to take a .txt file of 0's and 1's and convert it to a .bin file, of which I create a second program that takes that .bin file. I've been quite frustrated with this project, as my instructor just expects us to use AI to code it.

This project is coded in VSC, and complied via g++ in WSL. I've been able to figure out how to make the program take multiple arguments, with argv[1] being the input and argv[2] being the output. After that I've done a simple file stream to read to the end of the file line by line (the txt file is 32 bits per line) and cout it for testing purposes.

#include <iostream> #include <fstream> using namespace std; int main(int argc, char* argv[]) { if (argc != 2) { cout << "2 Command Line Arguments, Please." << endl; return 1; } ifstream inFS; string out; inFS.open(argv[1]); if (!inFS.is_open()) throw runtime_error("Error opening file"); while (getline(inFS,out)) { inFS >> out; cout << out << endl; } inFS.close(); return 0; }

Two biggest issues I am running into at the moment:

1.) when I go to test this code g++ myprogram.cpp t1.txt out I get:

/usr/bin/ld:t1.txt: file format not recognized; treating as linker script /usr/bin/ld:t1.txt:1: syntax error collect2: error: ld returned 1 exit status

2.) How do I take the bits and write them to a binary file? I feel like getline() is wrong, as I'd want to take them in individually, and then do something to them whether the bit is a 0 or 1.

Read Entire Article