这是我要做的。以文本文件为例(如下所示)
test.txt
The quick brown fox jumped over the lazy dog我需要将该文件按字节分割成任意的除法。所以上面的文件是45个字节(包括EOL/EOF字符)。我基本上是想用任意的方式按字节来分割它。
所以,如果我把它分成4部分,我就会得到这样的结果:
电流
Part1:快速b(11个字节)
Part2:(11个字节)
Part3:超过t(11个字节)
Part4:懒惰的狗(12字节)
(大致如此)
但是我想把它分割成完整的词,这样看起来就像这样
期望的
Part1:快速棕色(15字节)
Part2: fox跳转(9个字节)
Part3:超过(8个字节)
Part4:懒惰狗(9个字节)
或者大致类似的东西,这样划分就有了完整的单词。如果有3个单词和6个部分要分割,前3个应该有一个单词,其余的应该是空的。就像这样:
档案:速成棕色
(分为6部份)
Part1:
Part2:快
Part3:布朗
第4-6部分:“
这是我所拥有的,它给了我“电流”
// Get file size in bytes
off_t fileSize = statBuf.st_size;
// Split a section of file to read for each thread
off_t startSection[NUM_SECTIONS];
off_t endSection[NUM_SECTIONS];
for (int i = 0; i < NUM_SECTIONS; i++) {
if (i == 0) {
// Start at 0, end at our interval chunk
startSection[i] = 0;
endSection[i] = fileSize / NUM_SECTIONS;
} else {
// Start at the last section's end
startSection[i] = endSection[i-1];
// End after the next chunk
endSection[i] = (fileSize / NUM_SECTIONS) * (i + 1);
}
// At the last section, add any remaining bytes
if (i == NUM_SECTIONS - 1) {
endSection[i] += fileSize % NUM_SECTIONS;
}
}我认为我必须查看文件内容并识别空格/标点符号(我想将标点符号和空白字符视为相同)。但我无法让它在相同的部分实现(任意,可以是3部分,4,5,6等等)
任何帮助都是非常感谢的。这也是在linux上。
发布于 2014-05-02 16:37:15
如果您事先知道文件的大小,那么这个方法将是一个很好的起点,我认为(仅限C-ish伪代码):
filesize = ???;
nchunks = ???;
fileno = 1;
bytes_processed = 0;
while (bytes_processed < filesize)
{ copy_one_byte();
if (++bytes_processed >= (filesize / nchunks * fileno))
{ // keep processing to end of word or the end of file, whichever is first
// then switch to next file
++fileno;
}
}https://stackoverflow.com/questions/23431299
复制相似问题