C++ reading leftover data at the end of a file

I am taking input from a file in binary mode using C++; I read the data into unsigned ints, process them, and write them to another file. The problem is that sometimes, at the end of the file, there might be a little bit of data left that isn't large enough to fit into an int; in this case, I want to pad the end of the file with 0s and record how much padding was needed, until the data is large enough to fill an unsigned int.

Here is how I am reading from the file:

std::ifstream fin;
fin.open('filename.whatever', std::ios::in | std::ios::binary);
if(fin) {
    unsigned int m;
    while(fin >> m) {
        //processing the data and writing to another file here
    }
    //TODO: read the remaining data and pad it here prior to processing
} else {
    //output to error stream and exit with failure condition
}

The TODO in the code is where I'm having trouble. After the file input finishes and the loop exits, I need to read in the remaining data at the end of the file that was too small to fill an unsigned int. I need to then pad the end of that data with 0's in binary, recording enough about how much padding was done to be able to un-pad the data in the future.

How is this done, and is this already done automatically by C++?

NOTE: I cannot read the data into anything but an unsigned int, as I am processing the data as if it were an unsigned integer for encryption purposes.

EDIT: It was suggested that I simply read what remains into an array of chars. Am I correct in assuming that this will read in ALL remaining data from the file? It is important to note that I want this to work on any file that C++ can open for input and/or output in binary mode. Thanks for pointing out that I failed to include the detail of opening the file in binary mode.

EDIT: The files my code operates on are not created by anything I have written; they could be audio, video, or text. My goal is to make my code format-agnostic, so I can make no assumptions about the amount of data within a file.

EDIT: ok, so based on constructive comments, this is something of the approach I am seeing, documented in comments where the operations would take place:

std::ifstream fin;
fin.open('filename.whatever', std::ios::in | std::ios::binary);
if(fin) {
    unsigned int m;
    while(fin >> m) {
        //processing the data and writing to another file here
    }
    //1: declare Char array
    //2: fill it with what remains in the file
    //3: fill the rest of it until it's the same size as an unsigned int
} else {
    //output to error stream and exit with failure condition
}

The question, at this point, is this: is this truly format-agnostic? In other words, are bytes used to measure file size as discrete units, or can a file be, say, 11.25 bytes in size? I should know this, I know, but I've got to ask it anyway.


Are bytes used to measure file size as discrete units, or can a file be, say, 11.25 bytes in size?

No data type can be less than a byte, and your file is represented as an array of char meaning each character is one byte. Thus it is impossible to not get a whole number measure in bytes.


根据你的帖子,这里是第一步,第二步和第三步:

while (fin >> m)
{
    // ...
}

std::ostringstream buffer;
buffer << fin.rdbuf();

std::string contents = buffer.str();

// fill with 0s
std::fill(contents.begin(), contents.end(), '0');
链接地址: http://www.djcxy.com/p/54084.html

上一篇: 为什么编译器在N个字节边界上对齐N个字节的数据类型?

下一篇: C ++读取文件末尾的剩余数据