Domanda

Reading csv data into buffer. Trying to read the buffer to change the delimiter to null character and get access violation after around the 3.5 millionth character. There are over 14 million characters in the file. What kind of voodoo gypsy magic is this??

void getCurrentData(FILE *current){

    int totalProducts = 0, totalChars = 0, colCount = 0, 
        next = '\0', ch = '\0', productCount = 0;
    long *buffer = NULL;
    long i = 0;

    fseek(current, 0, SEEK_END);
    long fileSize = ftell(current);
    rewind(current);

    buffer = malloc(fileSize + 1);
    fread(buffer, fileSize, 1, current);

    /*replacing delimiter with null character*/
    while (ch != EOF){
        if (ch == ',' && next != ' '){
            buffer[i - 1] = '\0';
        }
        ch = next;
        if (next != EOF){
            next = (int)(buffer)[i];     /*i was violated here*/
            i++;
        }
    }
}
È stato utile?

Soluzione

Your fread is writing into a buffer of longs; so you'd expect only 3.5 million (= 14 million / sizeof(long)) items to be written; but you're accessing buffer as if you expect it to be a character array. Accessing the 3.5+ millionth item walks off the end of the array, since you malloc'ed it to be 14 million bytes, not 14 million entries.

In addition, fread never puts EOF into the result it writes into the buffer; it writes out the contents of the file unchanged. You must look at the return value of fread to see how much was actually read.

Altri suggerimenti

If the access violation is at i = 3500000 instead of the 3,500,000 character, then this is at the end of your long buffer.

Since you don't know, if any of the characters form a long -1 value, you could easily go beyond your allocated buffer.

Because you're reading chars the variable buffer it would have to be as char or unsigned char, isn't it ? On this line

next = (int)(buffer)[i];     /*i was violated here*/

It depends on the machine you are, you're accessing 4 bytes/chars in machines of 32bits or 8 bytes in machines of 64 bits in this type of access due the buffer variable is type long.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top