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

问题描述 投票:0回答:1

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

我正在尝试将文件保存为字符串,这不是.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://imgur.com/a/1uQzOPq

提前谢谢您

c++ ascii special-characters getline istream
1个回答
0
投票

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

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

再见,塞缪尔

© www.soinside.com 2019 - 2024. All rights reserved.