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