Getline(); 提取以 1A 字符停止

Getline(); extraction stops with the 1A character

本文关键字:1A 字符 提取 Getline      更新时间:2023-10-16

所以我不知道如何正确解释这一点,但我会尽力而为。

我正在尝试将文件保存到字符串中,它不是一个.txt文件,它是一个 .umsbt 文件,所以它有奇怪的 ASCII 字符(如 00、0A、0E、1A...(所以当我使用getline();将 .umsbt 保存到字符串中,然后使用cout打印它时,整个文件没有打印,当我通过 HxD(十六进制编辑器(打开实际文件时,我看到打印在 1A 字符之前停止, 做了一些测试,这是1A角色的错。

这是我的代码

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <string.h>
#include <conio.h>
#include <sstream>
using namespace std;
string line;    //the file is stored here
string name; //name of the file

void printfile() {
int o = 0;
int fileCount;
ifstream file;
file.open(name, ios::in);
if (file.fail()) {
cout << "Not found n";
cin.get();
cin.get();
exit(1);
}else {
file.seekg(0, ios::end);
fileCount = file.tellg();
file.seekg(0);
while (!file.eof()) {

getline(file, line);
cout << "file character count: " << fileCount << endl;
cout << "string character count: " << line.length() << "n" << endl;

for (int i = 0; i < line.length(); i++) {
cout << line[i];
o++;
if (o == 16) {
cout << "n";
o = 0;
}
}
}
file.close();
}
}
int main()
{
cin.ignore(26, 'n');
cout << "Write the name of your file (.umsbt included) n" << endl;
cin >> name;
cout << "n";
printfile();
cin.get();
cin.get();
return 0;
}

希望有人可以帮助我,我目前正在尝试删除/替换任何文件中的所有 1A 字符,限制是您必须在 ifstream 本身中执行此操作,因为您无法将其保存在字符串中(会导致 1A 出现问题,文件不会完全保存(

(这是在HxD中打开的文件的图片,希望您了解它 https://i.stack.imgur.com/mpvY8.jpg(

提前谢谢你

似乎您使用的是二进制文件而不是普通的文本文件。 查看这些链接以了解二进制文件。

https://study.com/academy/lesson/writing-reading-binary-files-in-c-programming.html https://computer.howstuffworks.com/c39.htm

再见 塞缪尔