c seekg似乎返回一个十六进制地址而不是.txt文件中的实际char
发布时间:2020-12-16 06:51:44 所属栏目:百科 来源:网络整理
导读:我得到了一个简单的代码剪切,我正在尝试学习如何使用c库读取纯文本.在与程序相同的目录中,我得到了包含ASCII纯文本行的text1.txt.我运行代码后,我希望从textOut.txt的text1.txt获取相同的字符.相反,在textOut.txt我有100行 0x7ffdf21fd018 0x7ffdf21fd018 0x
|
我得到了一个简单的代码剪切,我正在尝试学习如何使用c库读取纯文本.在与程序相同的目录中,我得到了包含ASCII纯文本行的text1.txt.我运行代码后,我希望从textOut.txt的text1.txt获取相同的字符.相反,在textOut.txt我有100行
这是代码: #include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
int main() {
fstream afile;
afile.open("text1.txt",ios::in );
ofstream outfile;
outfile.open("textOut.txt");
int counter=0;
for( counter=0;counter<100;counter++ ){
outfile << afile.seekg(counter);
outfile << "n";
//printf("%dn",counter);
}
return 0;
}
解决方法seekg returns *this,因此令人惊讶的是<<运算符在这种情况下工作. 而是使用
outfile << static_cast<char>(afile.get()); 完整计划: #include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
int main()
{
fstream afile;
afile.open("text1.txt",ios::in);
ofstream outfile;
outfile.open("textOut.txt");
int counter=0;
for (counter=0; counter<100; counter++) {
afile.seekg(counter);
outfile << static_cast<char>(afile.get());
//outfile << afile.seekg(counter);
outfile << "n";
//printf("%dn",counter);
}
return 0;
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
