Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 
";

    std::cout<<"
ASCII(STRING): ";

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

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

    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<<"
"; 
        //resets the iterators
        j = 0;
        k = 8;

    }


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

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

    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
237 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...