Search code examples
c++asciispecial-charactersgetlineistream

Getline(); extraction stops with the 1A character


so I don't know how to explain this correctly but gonna try my best.

I'm trying to save a file into a string, it's not a .txt file, it's a .umsbt file so it has weird ASCII characters (like 00, 0A, 0E, 1A...) so when I save the .umsbt into the string using getline(); and then print it using cout, the whole file isn't printed, and when I open the actual file through HxD (hex editor) I see that the print stopped before a 1A character, did some tests and it's the 1A character's fault.

This is my code

#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;
}

Hope someone can help me, I'm currently trying to remove/replace all of the 1A characters in any file, and the restriction is that you have to do it in the ifstream itself, cause you can't save it in a string (will cause the problem with 1A and the file won't be fully saved)

(Here's a pic of the file opened up in HxD hope you get an idea of it https://i.sstatic.net/mpvY8.jpg)

Thank you in advance


Solution

  • Seems that you are using binary files and not a normal text file. Check out these links to learn about binary files.

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

    Bye, Samuel