I'm trying to read a file very quickly. I My tests have shown that reading line by line with "getline()" is slower than seeking to the end of the file and then reading it all at once with fread. Right now, I'm trying to get fread to work properly.
Test file:
abcdefghijklmnop3.14159265358979hydrogen, helium
My test code, taken from several sources:typedef uint8_t using_type;FILE* pFile = fopen(path,"r");fseek(pFile,0,SEEK_END);long lSize = ftell(pFile)/sizeof(using_type);rewind(pFile);using_type* buffer = (using_type*)malloc(sizeof(using_type)*lSize);if (buffer == NULL) { fputs("Memory error",stderr);}// copy the file into the buffer:size_t result = fread(buffer,1,lSize,pFile);if (result != lSize) { fputs("Reading error",stderr);}fclose (pFile);free (buffer);
"abcdefghijklmnop3.14159265358979hydrogen, heliumÍÍýýýý««««««««"
However, the second error trips (because result==50 and lSize==52). When using "rb" mode, result==lSize==52, but the contents of "buffer" are garbage:0x00d960c8 "îþîþîþîþîþîþîþîþ...[etc.]...îþîþîþîþîþîþîþîþîþ{x"
Help? Thanks,
-G