Qt 5 Read a Text File With Delimiters
-
To find out how to read a text file with delimiters, go here:
http://tomtheprogrammer.wordpress.com/2014/11/08/qt-5-read-a-text-file-with-delimiters/
-
The source code you have posted is either incomplete or of poor quality.
You create global variables in the header file.
The header file is in no way connected with the source file - either deliberately, in which case you should have said that the code is just a concept or not...
You allocate memory on the heap for big elements - and assume all will be needed (e.g. QString static array of 3000 elements).
You wrote two void functions that don't return a single warning or error message.
You meld graphical user interface with the business logic - why ask user if he wants to copy the file only because the function didn't find it where it was hard-coded to look?
What is the reason of reading from a source file if it occurred that even the directory does not exist?
You provide little to no commentary at all - newbies will be overwhelmed by the amount of badly formatted and indented code.
You could have created a command-line application out of this file-reading function you presented, with argument being the source file and presenting parts of data in the prompt.
Sorry if I sounded too harsh, it is not my intent to insult you. I hope you can relate to what I have stated above and improve your code if you agree with at least some parts.
By the way, it could belong to the "Qt project wiki":http://qt-project.org/wiki/ or to a "Doc Note":http://qt-project.org/blog/view/doc-notes-we-have-a-vision.
-
-
The global variables are presented as a way to demonstrate object orientation, with class attributes and class methods. The size is only for demonstration on how to read large files. I have authored a program that uses these sizes because it reads a text file that currently has 1600 lines, and I gave it room to grow without having to recompile and redistribute the program too often. I thought it was an easier example that using pointers. The class variables are initialized in the reading method, and used by other methods in the class. The class instance occurs when pressing a button, then several methods operate on the data, and then the memory disappears when the button_click method finishes executing.
-
I didn't see a need to have the method return error messages when the class method can handle it. Why create overhead for the calling main program unless necessary.
-
I used the example of directory creation and file copying to demonstrate how a data file that is distributed with the application can be written to a user area so the user has access to the data files and can edit them if chosen to. I actually use this in my application. Windows does not allow editing of files from the executable's start-up folder.
-
I'll work on improving the commentary and the indentation. Wordpress makes it hard to work with indentation (you have to use html code when posting the text).
-
This is all original code I use in my working application. The intent was to show how to parse line by line a delimited text file, but I wanted to give a complete example. I am a new developer so I am improving day by day. I welcome your comments and I will consider them as provided by you with the best of intentions, which is proving searchers on the web good examples of working, well presented and documented code. Thank you for your well constructed critique and I consider the input valuable.
Edit: More comments have been added and indentation improved.
-
-
Hi, some suggestions:
[quote author="te777" date="1369784489"]1. The global variables are presented as a way to demonstrate object orientation, with class attributes and class methods...
....but I wanted to give a complete example.[/quote]I didn't realize that you intended to show member variables (NOT global variables!) until I read your explanation here. What you posted in Myclass.h looks like standalone variables.
Give a complete example by showing the full class declaration, e.g.
@
class MyClass {
public:
MyClass();
void readTextFile();private:
int linestotal;
bool inputfilefound;// include other functions/variables as necessary
};
@[quote]The size is only for demonstration on how to read large files. I have authored a program that uses these sizes because it reads a text file that currently has 1600 lines, and I gave it room to grow without having to recompile and redistribute the program too often.[/quote]Use an array that can grow dynamically (e.g. QVector or a std::vector), instead of a fixed-size C array.
[quote]3. I used the example of directory creation and file copying to demonstrate how a data file that is distributed with the application can be written to a user area so the user has access to the data files and can edit them if chosen to. I actually use this in my application. Windows does not allow editing of files from the executable's start-up folder.[/quote]Keep in mind that you have to do a balancing act: You want to show a complete example, but at the same time you want to avoid showing things that are unrelated to your topic of "Read a Text File With Delimiters". Those can become distractions for a beginner who's trying to understand your core topic.
[quote]4. I’ll work on improving the commentary and the indentation. Wordpress makes it hard to work with indentation (you have to use html code when posting the text).[/quote]You can get plugins that are designed for showing code in WordPress sites: http://stackoverflow.com/questions/2627527/good-wordpress-theme-plugin-for-showing-off-code-examples-and-scripts
A well-formatted example is much easier to read and understand.
[quote]I am a new developer so I am improving day by day. I welcome your comments and I will consider them as provided by you with the best of intentions, which is proving searchers on the web good examples of working, well presented and documented code.[/quote]It's good to see your willingness to share your knowledge, and your commitment to improve. All the best! :)