Using fread to Read a Text File

Started by
3 comments, last by Zahlman 13 years, 3 months ago
Hi,

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);
When using "r" mode, the contents of "buffer" seem to be somewhat meaningful:
"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

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

Advertisement
In "r" mode, the difference between result and lSize seems to be the number of newlines in the file. However, removing all newlines results in garbage again!

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

i have similar problem but have solved it with a parser.
The problem is that windows texteditors dont use \n to mark new lines.
They use \n\r to mark newlines.

If im right, the binarymode reads both signs \n\r and the textmode jut reads \n
I think the trouble was the carriage returns. Thanks!

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

Moving to For Beginners.

You are doing a lot of strange and probably wrong things here, and I can't begin to imagine how you can simultaneously (a) require maximum performance reading from a file and (b) not be able to fix it yourself and have a complete understanding of what is going on.

This topic is closed to new replies.

Advertisement