c++seekg似乎返回了一个十六进制地址,而不是.txt文件中的实际字符
c++ seekg seems to be returning a hex address instead of the actual char from .txt file
我剪下了一段简单的代码,我正在学习如何使用c++库阅读纯文本。在与程序相同的目录中,我得到了包含ASCII纯文本行的text1.txt
。在我运行代码后,我希望在textOut.txt
从text1.txt
获得相同的字符。相反,在textOut.txt中,我有100行
0x7ffdf21fd0180x7ffdf21fd0180x7ffdf21fd0180x7ffdf21fd0180x7ffdf21fd0180x7ffdf21fd0180x7ffdf21fd0180x7ffdf21fd0180x7ffdf21fd0180x7ffdf21fd0180x7ffdf21fd0180x7ffdf21fd0180x7ffdf21fd0180x7ffdf21fd0180x7ffdf21fd0180x7ffdf21fd018
这是代码:
#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
返回*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;
}
相关文章:
- 如何将 txt 文件中的行分隔为组件C++
- 无法打开.txt文件
- 如何使用CLion在Mac上创建一个新的.txt文件
- 使用fstream对txt文件中的数字进行循环
- Visual Studio在尝试读取resource.txt文件时崩溃
- 如何从txt文件中读取多个不同长度的数组?
- 在一次迭代中从 txt 文件中读取多行
- 通过比较C++中的行在 txt 文件中搜索的最简单方法是什么?
- 写入超过 1GB 的.txt文件时出现奇怪的行为
- 视觉 如何循环访问C++中遵循类似格式的多个.txt文件?
- txt 文件中浮点数的最大和最小值
- 从.txt文件中读取浮点型数字并在公式中使用它们
- C++将文本从.txt文件解析为变量
- 在树结构.txt文件中搜索节点
- 为什么我无法打开/读取从 Python 调用的 C 扩展名中的 txt 文件?
- 在 txt 文件中显示前两个数字的程序
- 如何从具有两列的.txt文件创建并行数组?
- 如何使用c ++读取带有非英文字母的*.txt文件?(国际化)
- 如何像在 C++ 中处理数组一样对 .txt 文件中的字符进行寻址?
- 如何读取 TXT 文件并仅将 int 存储在 C 中