我有一个很大的字符数组
data
strncpy
就这么多。然而,当我去打印出来,我没有输出。一定有点小问题,但我看不出是怎么回事。
char* data;
int blocks[100], block_sizes[100];
int traversed, block_idx, nl_chars, i, len, chars;
char* block;
// Determine output blocks
nl_chars = 0;
block_idx = 1;
blocks[0] = 0;
chars = 0;
len = strlen(data);
for (i = 0; i < len; i++) {
chars++;
if (data[i] == '\n') {
nl_chars++;
}
if (data[i] == '\0' || nl_chars == 30) {
blocks[block_idx] = i;
block_sizes[block_idx - 1] = chars;
block_idx++;
nl_chars = 0;
chars = 0;
}
}
// Output blocks to stdout
traversed = 0;
while (traversed < block_idx) {
block = malloc(block_sizes[traversed] * sizeof(char));
if (block == NULL) {
error("[-]Unable to allocate required memory");
}
strncpy(block, data + blocks[traversed], block_sizes[traversed]);
printf("%s", block);
free(block);
while (1) {
memset(input, 0, BUFFER_SIZE);
fgets(input, sizeof(input), stdin);
fflush(stdin);
if (input[0] == '\n' || input[0] == '\r') {
break;
}
}
traversed++;
}