0

I'm trying to adapt an "ASCII to a binary" solution found here, to my task.

This solution read the line, which the user enters. For me, I'm looking to read a file line by line and assign every line to this solution, in order to convert it, then write to the binary output file.

The code works fine, but when I open the binary file, I find only the first line converted. any idea?

Thank you for your time.

#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <math.h>
#include <fstream>
#include <string.h>

int main(int argc, char const *argv[])
{

    int digit[1000], len, base(2), rem[8], j(0), k(8);
    std::string ch;

std::ofstream writeFile;
    if (argv[1])
    {

    std::ifstream infile(argv[1]);
    std::cout << "good \n";

    std::cout<<"\nASCII(STRING): ";

while (!infile.eof())
{
    std::getline(infile, ch);

    //calc the length of ch
    len = ch.length();
    std::cout << len <<"\n";

    for(int i=0; i<len; i++){
        digit[i] = static_cast<int>(ch[i]);
    }

    //file stream section begins here
    // std::ofstream writeFile;
    writeFile.open("ASCII-BINARY.bin");

   // std::cout<<"BINARY: ";
    for(int m=0; m<len; m++){
        while(digit[m]>0||j<8){
            //finds the remainder of the number
                rem[j] = digit[m]%base;
            //divides the number by the base value choosed
                digit[m] = digit[m]/base;
            //increments the array value
                j++;
            }

        while(k>0){
                //outputs the binaries
                std::cout<<rem[k-1];
                writeFile<<rem[k-1];
                //decrements the array value
                k--;
            }
        std::cout<<" "; //output the space after binary value here
        writeFile<<" ";
        //std::cout<<"\n"; 
        //resets the iterators
        j = 0;
        k = 8;

    }


}
 writeFile.close();
    //file stream section ends here
std::cout << "good close \n";

    }
    else std::cerr << "can't open file ! \n" ; 

    return 0;
}
MGM
  • 33
  • 6
  • 3
    To begin with, please read [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) To continue, I suggest you [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Apr 10 '18 at 10:47
  • 3
    And when you used your debugger to run your program one line at a time, and examine the values of all variables on each step, letting you see exactly what your program is doing, what observations did you make? – Sam Varshavchik Apr 10 '18 at 10:48
  • SO is not about guidance nor is it a code writing service. It's about specific bite sized programming Q&A. – Ron Apr 10 '18 at 10:56
  • "any idea?" Could you trim down the code to the [Minimal, Complete, Verifiable example](https://stackoverflow.com/help/mcve)? Right now, there is a lot of irrelevant stuff in the code snippet. And, I suspect, if you trim it down you'll find the bug along the way. – Eljay Apr 10 '18 at 13:50

0 Answers0