下面的代码可以工作,但是效率是使用(linux)管道将解压缩的数据提供给(修改过的)程序时的两倍。我需要一个稳定的流在程序中,我可以继续分裂\n。有没有办法用(字符串?)流还是其他什么把戏?

int main(int argc, char *argv[]) {
static const int unzipBufferSize = 8192;
long long int counter = 0;
int i = 0, p = 0, n = 0;
int offset = 0;
char *end = NULL;
char *begin = NULL;
unsigned char unzipBuffer[unzipBufferSize];
unsigned int unzippedBytes;
char * inFileName = argv[1];
char buffer[200];
buffer[0] = '\0';
bool breaker = false;
char pch[4][200];
Read *aRead = new Read;
gzFile inFileZ;
inFileZ = gzopen(inFileName, "rb");
while (true) {
    unzippedBytes = gzread(inFileZ, unzipBuffer, unzipBufferSize);
    if (unzippedBytes > 0) {
        unzipBuffer[unzippedBytes] = '\0'; //put a 0-char after the total buffer
        begin = (char*) &unzipBuffer[0]; // point to the address of the first char
        do {
            end = strchr(begin,(int)'\n'); //find the end of line
            if (end != NULL) *(end) = '\0'; // put 0-char to use it as a c-string
            pch[p][0] = '\0'; \\ put a 0-char to be able to strcat
            if (strlen(buffer) > 0) { // if buffer from previous iteration contains something
                strcat(pch[p], buffer); // cat it to the p-th pch
                buffer[0] = '\0'; \\ set buffer to null-string or ""
            }
            strcat(pch[p], begin); // put begin (or rest of line in case there was a buffer into p-th pch

            if (end != NULL) { // see if it already points to something
                begin = end+1; // if so, advance begin to old end+1
                p++;
            }

            if(p>3) { // a 'read' contains 4 lines, so if p>3
                strcat(aRead->bases,pch[1]); // we use line 2 and 4 as
                strcat(aRead->scores,pch[3]); // bases and scores
                //do things with the reads
                aRead->bases[0] = '\0'; //put them back to 0-char
                aRead->scores[0] = '\0';
                p = 0; // start counting next 4 lines
            }

        }
        while (end != NULL );
        strcat(buffer,pch[p]); //move the left-over of unzipBuffer to buffer
    }
    else {
        break; // when no unzippedBytes, exit the loop
    }
}

最佳答案

您的主要问题可能是标准的C字符串库。
使用strxxx()函数时,每次调用都要在整个缓冲区中迭代多次,首先是针对strchr(),然后是针对strlen(),然后是针对每个strcat()调用。
使用标准库是件好事,但在这里,它的效率很低。
如果你能想出一些更简单的方法,只触及每个字符一次,比如(代码只是为了显示原理,不要期望它起作用):

do
{
    do
    {
       *tp++ = *sp++;
    } while (sp < buffer_end && *sp != '\n');

    /* new line, do whatever it requires */
    ...
    /* reset tp to beginning of buffer */
} while (sp < buffer_end);

10-06 01:53