| | |
The Fastest way to read a .txt File
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2008
Posts: 119
Reputation:
Solved Threads: 10
•
•
•
•
I hope you realize that the code in the last line above will likely cause buffer overflow. Lets say howmuchIread == READSIZE, which is the same as sizeof(buffer). Then buffer[howmuchIread] will be one byte beyond the end of the buffer.C++ Syntax (Toggle Plain Text)
int howmuchIread = 0; while ( ( howmuchIread = fread ( buffer, 1, READSIZE, fp ) ) != 0 ) { buffer[ howmuchIread ] = 0; //place terminal character to avoid overruns
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
If you are using MFC you can try something like:
C++ Syntax (Toggle Plain Text)
CFile inFile("test.txt", CFile::modeRead); CArchive archive(&inFile, CArchive::load, inFile.GetLength()); CString line; while (archive.ReadString(line)) { // do something with <line> here } archive.Close(); inFile.Close();
If you are forced to reinvent the wheel at least try to invent a better one!
Please use code tags - Please mark solved threads as solved
Please use code tags - Please mark solved threads as solved
•
•
Join Date: Jan 2008
Posts: 119
Reputation:
Solved Threads: 10
•
•
•
•
If you are using MFC you can try something like:
C++ Syntax (Toggle Plain Text)
CFile inFile("test.txt", CFile::modeRead); CArchive archive(&inFile, CArchive::load, inFile.GetLength()); CString line; while (archive.ReadString(line)) { // do something with <line> here } archive.Close(); inFile.Close();
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
Yes, but loading the whole file in a single step makes it very fast. I agree, for files of a several 100 MB or more I wouldn't recommend this method, too.
If you are forced to reinvent the wheel at least try to invent a better one!
Please use code tags - Please mark solved threads as solved
Please use code tags - Please mark solved threads as solved
And one would surmise that you are not simply copying the input to an output, that you are doing some sort of manipulations. This is probably very key to answering your question in full. Rather than micro-optimizing each particular function call, work on the overall algorithm. Or at least present an overview so that better answers for your overall effort may come as a result.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
You are completely right about that. I have a large amout of code that I use after I red the values from the textfile. I mainly use the std:: namespace to substring, convert from text-number-text, like stringstream does, std::string::size_type, string1.length() etc...
I have experiment with this all day and found out that the System:: namespace have a much better performance than std:: namespace regarding the conversions and search in strings that I do. So I will have a great job to exchange all these operations and this will also improve performance and speed. However very nice.
As this example does this loop in 0.9 sec while the stringstream conversion that I use now will do this in 22 seconds.
As I have discovered this better performance above with System:: I thinking of testing to also read a file with the System:: namespace. However I have never done that before and have a bit of a problem to find an example of how to do that.
I think I should use: System::IO:: StreamReader
if I would read the file that I have now with ifstream and getline as an example, it would look like this. How would this example look like with the System::IO namespace.
Still searching google for this.
I have experiment with this all day and found out that the System:: namespace have a much better performance than std:: namespace regarding the conversions and search in strings that I do. So I will have a great job to exchange all these operations and this will also improve performance and speed. However very nice.
As this example does this loop in 0.9 sec while the stringstream conversion that I use now will do this in 22 seconds.
C++ Syntax (Toggle Plain Text)
double Number1; String^ Num = "4.34"; for(int i = 0; i < 2000000; i++) { Number1 = System::Convert::ToDouble(Num); } MessageBox::Show("Finish");
As I have discovered this better performance above with System:: I thinking of testing to also read a file with the System:: namespace. However I have never done that before and have a bit of a problem to find an example of how to do that.
I think I should use: System::IO:: StreamReader
if I would read the file that I have now with ifstream and getline as an example, it would look like this. How would this example look like with the System::IO namespace.
Still searching google for this.
C++ Syntax (Toggle Plain Text)
std::string Text; double n1, n2; char Comma; ifstream ReadFile("C:\\File.txt"); while( getline(ReadFile, Text, ',') ) { ReadFile >> n1; ReadFile >> Comma; ReadFile >> n2; ReadFile.get(); }
•
•
•
•
And one would surmise that you are not simply copying the input to an output, that you are doing some sort of manipulations. This is probably very key to answering your question in full. Rather than micro-optimizing each particular function call, work on the overall algorithm. Or at least present an overview so that better answers for your overall effort may come as a result.
Last edited by Jennifer84; Sep 25th, 2008 at 8:04 pm.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Compilation error
- Next Thread: system call to read the windows kernel message queue
Views: 6785 | Replies: 35
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






