I am generating random blobs (which in my case is a std::string made out of a std::vector<unsigned char> of arbitrary non-zero length). Blobs are random in the sense that they might be of any size within a given size range, and they contain random unsigned characters.
I am trying to write these blobs to a file(say Blobs.txt) in such a way that each line in my file will give me one of those generated blobs. For example, the first line of Blobs.txt will contain the first generated blob, the last line of Blob.txt will contain the last generated blob and so on.
The problem that I'm currently facing is that the blob themselves might contain newline characters(\n). Hence, the number of lines in my Blobs.txt might not be equal to the number of blobs generated.
As blobs are random, I can not make any assumptions about how each of these blobs look like.
I see that the problem is being caused by the interference of two different use cases of the newline character in a single file, one in the blob content themselves and the other as a separator in the file. And, to solve this I either have to replace the newline character with something else in my blob content and then put them in the Blobs.txt, or, I have to change the blob separator in my Blobs.txt file from newline character to something else which can not appear in my blob content(not sure if the second solution is even possible).
I know that I am going to store a fixed number of blobs in Blobs.txt.
I was thinking of appending some explicit fixed length numbering prior to each blob in Blobs.txt file, e.g., "001:<blob 1 string>" but this does not seem to solve the problem because "001:" itself might be present in some blob, and we don't know the blob sizes beforehand.
One possible solution is to write the offsets in a separate file(say Blob_Offsets.txt) where I store the size of each of these blobs and use these to seek Blobs.txt. I can't want to rely on the in-memory structures because once they are gone, I won't be able to make sense out of Blobs.txt.
I was wondering if it is really needed to create a separate offset file because this will increase the number of disk accesses while reading a blob? Is there a better way to solve this problem?