将文件分割成相等的字节段,用完整字分隔(C/ c++)
Split file into equal byte sections, separated by complete word (C/C++)
我需要这样做。以文本文件为例(如下所示)
test.txt
The quick brown fox jumped over the lazy dog
我需要将该文件分割成任意的字节分割。所以上面的文件是45字节(包括EOL/EOF字符)。我基本上想以任意的方式按字节分割。
所以如果我把它分成4部分,我会得到这样的内容:
目前第1部分:快速b(11字节)
第二部分:小狐狸(11字节)
Part3: mped over t (11 bytes)
Part4: the lazy dog (12 bytes)
(大致如此)
但是我想把它分成完整的单词,所以它看起来像这样
的
Part1: The quick brown (15 bytes)
Part2: fox jump (9 bytes)
Part3: Over the (8 bytes)
Part4: lazy dog (9 bytes)
或者类似的东西,这样除法就有完整的词。如果有3个单词和6个部分要分割,那么前3个部分应该每个都有一个单词,其余部分应该是空的。这样的:
file: The quick brown
(分成6部分)
Part1:
第二部分:快
Part3:布朗
Part4-6: "
这是我得到的"current"
// 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等)
任何帮助都是感激的。
如果您事先知道文件的大小,我认为这种方法将是一个很好的起点(仅限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;
}
}
相关文章:
- 如何将 txt 文件中的行分隔为组件C++
- 无法通过空白将文本文件行分隔为矢量
- 如何在C++中读取空格分隔的输入 当我们不知道输入的数量时
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- 如何在 C++ 中将文件中的逗号分隔数字读取到数组中?
- "Warning: Comma within array index expression"但逗号分隔函数参数
- C++逗号分隔的输入数组代码过早退出
- 如何分隔字符串并将标记传递给方法
- 分隔整数 (C++) 的数字
- 野牛/yacc 解析器在不被空格分隔时跳过 grammer - "unexpected $end"
- 读取制表符分隔的文件时出现问题 C++
- 通过分隔符分隔包含 UTF-16 BE 文本的uint8_t数组
- 如何更新和删除文本文件中的特定逗号分隔值?C++
- 如何将逗号分隔的文件读取为 2D int 数组?
- 读取带有分隔C++的文件