0

Here my code:

//main.cpp

#include <iostream>
#include <fstream>  //files
#include <string>   //strings
#include <sstream>  //stringstreams

string intToString(int wert){
    ostringstream strout;
    string str;
    strout<<wert;
    str=strout.str();
    return str;}

int stringToInt(string str){
    istringstream strin;
    unsigned long long intVar;
    strin.str(str);
    strin>>intVar;
    return intVar;}

string wordsToAscii(string wort){
    string hold;
    for(int j=0;j<wort.length();j+=3){
        for(int i=j;i<j+3;i++){
            if(int(wort[i]>=100))
                hold=hold+intToString(int(wort[i]));
            if(int(wort[i]>=10 && wort[i]<=99))  
                hold=hold+"0"+intToString(int(wort[i]));
            if(int(wort[i]<=9))
                hold=hold+"00"+intToString(int(wort[i]));
        }
    }
    return hold;
}

string AsciiToWords(string wort){
    string hold;
    string total;
    for(int j=0;j<wort.length();j+=15)
        for(int i=j;i<j+15;i+=3){
            hold="\0";
            for(int k=i;k<i+3;k++)
                hold+=wort[k];
            if(hold=="000")
                break;
            total+=stringToInt(hold);
        }
    return total;
}

int main(){

    string str;

    ifstream f ("input");
    ofstream g ("temp");
    while(!f.eof())
        if(getline(f,str)){
            cout<<wordsToAscii(str)<<"\n";
            g<<wordsToAscii(str)<<"\n";}
    f.close();
    g.close();

    ifstream h ("temp");
    ofstream i ("output");
    while(!h.eof())
        if(getline(h,str)){
            cout<<AsciiToWords(str)<<"\n";
            i<<AsciiToWords(str)<<"\n";}
    h.close();
    i.close();

    return 0;
}

input: (file)

first line test1
second line test2
last line test3
testA testB testC
one
two

temp: (file)

102105114115116032108105110101032116101115116049000000
115101099111110100032108105110101032116101115116050000
108097115116032108105110101032116101115116051
116101115116065032116101115116066032116101115116067000
111110101
116119111

output: (file)

first line test1
second line test2
last line test3
testA testB testC
one

output: (in terminal)

102105114115116032108105110101032116101115116049000000
115101099111110100032108105110101032116101115116050000
108097115116032108105110101032116101115116051
116101115116065032116101115116066032116101115116067000
111110101
116119111
first line test1
second line test2
last line test3
testA testB testC
oneA
twoA

The first function converts caracters into their corresponding ASCII numbers. The second one is supposed to convert it back.

The two functions seem to work good. The problem is the different output in file and terminal. The only difference is cout<< instead of i<<

Also, with different input, sometimes the last line is written twice, or not written at all. I just can't explain it myself. I researched hours, changed the way of reading/writing to files, rewrote some parts of code etc. without finding the reason

Thanks in advance for help

westernCiv
  • 29
  • 6

2 Answers2

2

I corrected some mistakes and this version works for me. I tried to respect your way to do it, even thought it is a bit odd

I removed the intToString and stringToInt functions. Used the to_string static function from string instead. I removed the double loops in the wordsToAscii and AsciiToWords functions because they are useless and make it harder to see what's going on

I think the main problem here is the way that the files are read, just perform a while(getline(h,str)) is enough to read it until the end as pointed in another comment.

Hope this help !

string wordsToAscii(string wort){
    string hold;
    int ascii_value;

    for(int i=0 ; i < wort.length() ; i++){
        char car = wort[i];
        ascii_value = int(car);

        if( ascii_value >=100)
                hold=hold+ to_string(ascii_value);
        else if( ascii_value >=10 &&  ascii_value <=99)
                hold += "0"+ to_string(ascii_value);
        else
                hold += "00"+ to_string(ascii_value);
    }
    return hold;
}

string AsciiToWords(string wort){
    string hold;
    string total;
    int ascii_value;
    char car;

    for(int i=0 ; i<wort.size() ; i+=3){
        hold ="";
        for(int k=i;k<i+3;k++)
            hold+=wort[k];

        ascii_value = atoi(hold.c_str()); // Conversion of the string "105" to value 105
        car = ascii_value; //Conversion of the value 105 to corresponding ASCII character 'f'
        total += car;//Concatenate the character 'f' to string
    }
    return total;
}

int main(){

    string str;

    ifstream f ("C:\\input.txt");

    if(!f.is_open()){
        cout << "File not opened " << endl;
        return 0;
    }

    ofstream g ("temp");
    while(getline(f,str)){
        cout << wordsToAscii(str) << "\n";
        g<<wordsToAscii(str)<< "\n";
    }
    f.close();
    g.close();

    ifstream h ("temp");
    ofstream i ("output");
    while(getline(h,str)){
            cout << AsciiToWords(str) << "\n";
            i << AsciiToWords(str) << "\n";
    }
    h.close();
    i.close();

    return 0;
}
0

eof() returns true only after unsuccessful attempt to read from the end of file, when you use wrong construction while(!eof()) your code does extra iteration in a loop.

After this iteration there is garbage in variables that you have read. This garbage is different when you print data to console and to file, that is why you see different output

There are also some problems with functions asciiToWords and wordsToAscii, here is working after some changes:

//main.cpp

#include <iostream>
#include <fstream>  //files
#include <string>   //strings
#include <sstream>  //stringstreams

using namespace std;

string intToString(int wert){
    ostringstream strout;
    string str;
    strout<<wert;
    str=strout.str();
    return str;}

int stringToInt(string str){
    istringstream strin;
    unsigned long long intVar;
    strin.str(str);
    strin>>intVar;
    return intVar;}

string wordsToAscii(string wort){
    string hold;
    for(int i=0;i<wort.length();i++){
            if(int(wort[i]>=100))
                hold=hold+intToString(int(wort[i]));
            if(int(wort[i]>=10 && wort[i]<=99))  
                hold=hold+"0"+intToString(int(wort[i]));
            if(int(wort[i]<=9))
                hold=hold+"00"+intToString(int(wort[i]));
    }
    return hold;
}

string AsciiToWords(string wort){
    string hold;
    string total;
    for(int j=0;j<wort.length();j+=3)
    {
        hold="";
        for(int i=j;i<j+3;i++)
            hold+=wort[i];
        total+=stringToInt(hold);
    }    
    return total;
}

int main(){

    string str;

    ifstream f ("input");
    ofstream g ("temp");
    while(getline(f,str))
    {
            cout<<wordsToAscii(str)<<"\n";
            g<<wordsToAscii(str)<<"\n";
    }
    f.close();
    g.close();

    ifstream h ("temp");
    ofstream i ("output");
    while(getline(h,str))
    {
            cout<<AsciiToWords(str)<<"\n";
            i<<AsciiToWords(str)<<"\n";
    }
    h.close();
    i.close();

    return 0;
}
Sandro
  • 2,099
  • 2
  • 17
  • 27
  • That means I should use `while(getline(h,str))` ? It gives the same output like before. And the last line "two" is not printed at all... – westernCiv Dec 10 '16 at 17:29
  • what exactly functions asciiToWords and wordsToAscii do? – Sandro Dec 10 '16 at 17:41
  • "The first function converts caracters into their corresponding ASCII numbers. The second one is supposed to convert it back." `Hello = 072-101-108-108-111-000` . Zeros are appended to make `length()%9==0` true – westernCiv Dec 10 '16 at 17:43
  • why wordsToAscii uses loop with step 3, what if string length is not divided by 3 ? – Sandro Dec 10 '16 at 17:53
  • length of two first strings if file input is different, so length of two first strings in "temp" should be different too, but they are the same, so something is defenetely wrong in "wordsToAscii" – Sandro Dec 10 '16 at 18:02
  • It converts line by line, not string by string – westernCiv Dec 10 '16 at 18:10
  • first line is longer then second line in file "input" – Sandro Dec 10 '16 at 18:13
  • It is one character longer. That's why there are two `000` at the end, and just one `000` in the second line. – westernCiv Dec 10 '16 at 18:25