-1

I'm trying to read some text out of a file called "file.dat". The problem is, that the string in the file does not include a zero at the end as for standard C. So I need something that adds the zero, so I can work with the string without getting random symbols after the string when I print it.

void cSpectrum::readSpectrum(const std::string &filename, double 
tubeVoltage, double &minEnergy, std::string &spectrumName)
{
    //Object with the name "inp" of the class ifstream
    ifstream inp(filename, ios::binary);

    //Checks if the file is open
    if (!inp.is_open()) {
        throw runtime_error("File not open!");
    }

    cout << "I opened the file!" << endl;

    //Check the title of the file
    string title;
    char *buffer = new char[14];
    inp.read(buffer, 14);

    cout << buffer << endl;
}

At the moment I get the following output, I would like to get it without the ²²²²┘.

I opened the file!

x-ray spectrum²²²²┘

thefighter3
  • 39
  • 1
  • 2
  • 11
  • 2
    Maybe just use a `std::string` instead of all the manual memory management and `char*` gunk that you are currently doing. – Jesper Juhl Mar 27 '19 at 20:00
  • Is the file really binary? – ttemple Mar 27 '19 at 20:01
  • Is it really a binary file or is it a text file? If it's really a binary file, treating it like you can trust it's a text file is probably a problem. If you want to dump the contents, you probably should dump them one character at a time, because it might have, you know, binary data. If it's a text file, then you should open it as a text file and read into a string. However, Remy's answer isn't horrible. – Joseph Larson Mar 27 '19 at 20:01
  • Yes, it is a .dat file – thefighter3 Mar 27 '19 at 20:03
  • @thefighter3 "it is a .dat file" - the *name* of the file, including its extension (`.dat` in this case) means *nothing*. You can name a PNG file ".txt" if you want to, that doesn't turn the image into a text document. You cannot infer *anything* from the file name alone. – Jesper Juhl Mar 27 '19 at 20:28

2 Answers2

3

Simply allocate +1 more char for your array, but don't read into that char, just set it to 0:

char buffer[15];
inp.read(buffer, 14);
buffer[14] = '\0';
cout << buffer << endl;

Or, simply don't use a char[] at all, use std::string instead, see:

What is the best way to read an entire file into a std::string in C++?

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
0

I did it with the std::string now. If you want you can replace the 14 by an integer variable.

void cSpectrum::readSpectrum(const std::string & filename, double tubeVoltage, double 
        & minEnergy, std::string const & spectrumName){

    ifstream inp(filename, ios::binary);

    //Checks if the file is open
    if (!inp.is_open()) {
        throw runtime_error("ERROR: Could not open the file!");
    }

    //Reads the title
    string title(14, '\0');
    inp.read(&title[0], 14);

    //If it is not the correct file throw an ERROR
    if (title != spectrumName)
        throw runtime_error("ERROR: Wrong file title");

    readSpectrum(inp, tubeVoltage, minEnergy, spectrumName);
}
thefighter3
  • 39
  • 1
  • 2
  • 11