简体   繁体   English

在Win32中解析巨大的Char *缓冲区

[英]Parsing Huge Char* Buffer In Win32

I have some huge csv files and try read files, parsing and write in one DB. 我有一些巨大的csv文件,并尝试读取文件,解析和写入一个DB。 csv format is same below: csv格式如下:

EUR/USD 20100103 21:27:59.694   1.43067 1.43097
EUR/USD 20100103 21:27:59.732   1.43075 1.43095
EUR/USD 20100103 21:28:08.152   1.43078 1.43099
EUR/USD 20100103 21:28:16.897   1.43076 1.43102
EUR/USD 20100103 21:28:28.757   1.43071 1.43101
EUR/USD 20100103 21:29:07.659   1.43071 1.43106

And my C++ code is: 我的C ++代码是:

void Read(char* fileName){
  FILE * pFile;
        long lSize;
        char * buffer;
        size_t result;

        pFile = fopen (fileName , "rb" );
        if (pFile==NULL) {fputs ("File error",stderr); exit (1);}

        fseek (pFile , 0 , SEEK_END);
        lSize = ftell (pFile);
        cout<<lSize<<"\n";
        rewind (pFile);

        // allocate memory to contain the whole file:
        buffer = (char*) malloc (sizeof(char)*lSize);
        if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

        result = fread (buffer,1,lSize,pFile);
        if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

                char * pch;
        pch = strtok (buffer,"\n");

        while (pch != NULL) {

            Parsing(pch);
                pch = strtok (NULL, "\n");
        }

        free (buffer);
        fclose (pFile);
}

void Parsing(char* tmpp){
    char * pch;
    pch = strtok (tmpp,",");

    char temp[256];
        strcpy(temp, "INSERT INTO dukas  VALUES('" );
    while (pch != NULL) {
        cout<<pch<<"\n";
        pch = strtok (NULL, ",");

    }


}

Problem is that this file, only read first line and cant move to second line. 问题是该文件只能读取第一行,而不能移动到第二行。 Any bugs in this script? 这个脚本中有任何错误吗? Regards, 问候,

From the MSDN on strtok: within a single thread, interleaving calls to one of these functions is highly likely to produce data corruption and inaccurate results. 从MSDN上的strtok: 在单个线程中,对这些函数之一的调用交叉存取很可能导致数据损坏和不准确的结果。 When parsing different strings, finish parsing one string before starting to parse the next . 解析不同的字符串时,请先解析一个字符串,然后再开始解析下一个字符串

Which is exactly what you're doing: you have one strtok reading lines, another parsing fields inside the Parsing function. 这正是您正在执行的操作:您有一条strtok读取行,而在Parsing函数中有另一条解析字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM