gpt4 book ai didi

C++ 输出:std::cout 和文件输出的内容不同

转载 作者:行者123 更新时间:2023-11-28 01:56:20 24 4
gpt4 key购买 nike

这是我的代码:

//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;
}

输入:(文件)

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

临时:(文件)

102105114115116032108105110101032116101115116049000000
115101099111110100032108105110101032116101115116050000
108097115116032108105110101032116101115116051
116101115116065032116101115116066032116101115116067000
111110101
116119111

输出:(文件)

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

输出:(在终端中)

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

第一个函数将字符转换为相应的 ASCII 码。第二个应该将其转换回来。

这两个功能似乎都很好用。问题是文件和终端中的输出不同。唯一的区别是 cout<<而不是 i<<

另外,对于不同的输入,有时最后一行会被写两次,或者根本不写。我自己无法解释。研究了好几个小时,改了文件的读写方式,重写了部分代码等等都没有找到原因

在此先感谢您的帮助

最佳答案

我纠正了一些错误,这个版本适合我。我试着尊重你做这件事的方式,甚至觉得这有点奇怪

我删除了 intToString 和 stringToInt 函数。改为使用 string 中的 to_string 静态函数。我删除了 wordsToAscii 和 AsciiToWords 函数中的双循环,因为它们没有用,而且更难看出发生了什么

我认为这里的主要问题是读取文件的方式,只需执行 while(getline(h,str)) 就足以读取它直到另一条评论中指出的结束。

希望对您有所帮助!

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

关于C++ 输出:std::cout 和文件输出的内容不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41077802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com