Voici mon code:C++ sortie: std :: Cout et fichier de sortie ont pas le même contenu
//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;
}
entrée
: (fichier)
first line test1
second line test2
last line test3
testA testB testC
one
two
temp: (fichier)
8105110101032116101115116049000000
8105110101032116101115116050000
8105110101032116101115116051
116101115116065032116101115116066032116101115116067000
111110101
116119111
sortie: (fichier)
first line test1
second line test2
last line test3
testA testB testC
one
sortie: (dans le terminal)
8105110101032116101115116049000000
8105110101032116101115116050000
8105110101032116101115116051
116101115116065032116101115116066032116101115116067000
111110101
116119111
first line test1
second line test2
last line test3
testA testB testC
oneA
twoA
La première fonction permet de convertir en caractères ASCII leurs numéros correspondants. Le second est censé le reconvertir.
Les deux fonctions semblent fonctionner correctement. Le problème est la sortie différente dans le fichier et le terminal. La seule différence est cout<<
au lieu de i<<
De même, avec une entrée différente, parfois la dernière ligne est écrite deux fois, ou pas du tout écrite. Je ne peux pas l'expliquer moi-même. Je l'ai étudié heures, changé le mode de lecture/écriture de fichiers, réécrit certaines parties de code, etc., sans trouver la raison
Merci à l'avance de l'aide
[ 'while (! Eof())' est faux] (http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) . – melpomene
Attention à votre moût [i]. Dans votre code «je» peut être plus grande que la longueur de moût. – Ripi2
Que sont 'stringToInt' et' intToString'? – melpomene