C 输出:STD :: COUT和文件输出没有相同的内容
C++ Output: std::cout and file output have not the same content
在这里我的代码:
//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=" ";
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
temp :(文件)
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功能。改用字符串使用to_string静态函数。我删除了Wordstoascii和Asciitowords功能中的双重循环,因为它们没有用,并且很难看到发生了什么
我认为这里的主要问题是读取文件的方式,只需执行一段时间(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;
}
eof()只有在尝试从文件末尾读取读取失败后,当您使用错误的构造时(!
此迭代后,您阅读的变量中有垃圾。当您将数据打印到主机和文件时,此垃圾是不同的,这就是为什么您会看到不同的输出
功能也有一些问题,asciitowords和wordstoascii,在此后正在工作:
//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;
}
相关文章:
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- VI工作室代码错误无法打开输出文件主.exe
- 在代码块中查找输出文件
- 将未签名的字符写入输出文件
- 为什么我的代码不写入输出文件?
- 完成搜索任务时为空输出文件
- 如何将地图<列表<string>、矢量<string>>输出到输出文件?
- 无法在代码块 IDE 中打开输出文件.exe
- C++:更改 IF 语句中的输出文件
- 编译 gsoap 输出文件 (onvif) 时出现问题
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- while 循环中的流输出文件() 通常生成空文件
- 如何使用 Docker 访问输出文件
- 提升进程间共享内存删除、权限和输出文件
- 查找素数,然后将质数放入输出文件中
- GDB / C++ - 根据循环条件将多个变量记录到输出文件中
- 带有文件结束函数的 while 循环重复输出文件中的最后一个数字两次
- 我的输出文件未显示解决方案
- 输入文件中的第一个字符是输出文件中的最后一个字符,反之亦然
- C++,我正在尝试创建一个有效的.bmp输出文件