开发者

Random characters displayed when printing array of characters

This is my problem. I am reading a file in binary mode, appending the bytes to an int array, and printing the values after. My problem is, when I cout my results, random characters are being attached in the stream.

comp.txt:

this text is a testt1

main.cpp:

#include <iostream>
#include <fstream>
#include <time.h>


using namespace std;

void read(ifstream& 开发者_StackOverflow社区stream, unsigned int buf[], int size)
{
    for(int i = 0; i < size; ++i)
    {
        unsigned char temp[4] = {'\0', '\0', '\0', '\0'};
        stream.read((char*)temp, 4);
        cout << "Temp: " << temp << '\n';
        buf[i] = *((int*)temp);     
        cout << "Read: " << buf[i] << endl;
        memset(temp, '\0', 4);
    }
}

int main()
{

    // open file
    ifstream f;
    f.open("comp.txt", ios::binary);
    cout << "File opened. " << endl;

    // get size
    f.seekg(0, ios::end);
    int l = f.tellg();
    int length = (l / 4) + 1;
    f.seekg(0, ios::beg);
    cout << "Size found: L: " << l << " Length: " << length << endl;

    // allocate byte arrays
    unsigned int* buf = new unsigned int[length];
    memset(buf, '\0', 4*length);
    // unsigned short* key = new unsigned short[length];
    cout << "Preparing to read..." << endl;

    // read byte into short
    cout << "Reading..." << endl;
    read(f, buf, length);
    f.close();
    delete[] buf;
    cin.ignore(1000, 10);
    return 0;
}

Output:

C:\Users\daminkz\Desktop>encrypt
File opened.
Size found: L: 21 Length: 6
Preparing to read...
Reading...
Temp: this
Read: 1936287860
Temp:  tex☺
Read: 2019914784
Temp: t is☻
Read: 1936269428
Temp:  a t♥
Read: 1948279072
Temp: estt♦
Read: 1953788773
Temp: 1
Read: 49

Things to note:

  • temp is only 4 bytes, but 5 bytes are printed


When you read in temp, you overwrite all 4 characters with data, leaving you without a NULL terminator. cout.operator<<(char*) expects a null-terminated string, so it's printing as many characters as it can until it reaches a null terminator. Making temp be 5 characters long, all '\0', but keeping number of bytes read at 4 will alleviate the problem.


You are outputting temp as a null-terminated string, but it is not null-terminated, so you will print out 4 characters of temp and unknown number of garbage characters until you accidentally run into a 0.


You're not null-terminating the buffer you're printing.


When you pass a char array to cout it gets converted to a char pointer, in other words what is normally called a "C-string". This means that to find the end of the string the output routing will search for the first 0x00 NUL character.

Your array is 4 chars however, and 4 chars are read from the file. If none of these is a NUL then the output will keep reading chars from memory until it finds one and that are the strange chars you're observing. Note that another possible result is a segfault because you're not supposed to be able to go around and read memory that you didn't explicitly allocate.

As a solution you could declare your array of 5 chars instead of 4, leaving the fifth always at NUL to be sure to stop the output.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜