4 Ways to Read Files with C++
Among the examples includes: modern, classic, gamefile and parser!
In C/C++ programming and any other programming language, in many situations we need to read external files. Whether to get data, do * parser * and among other features.
In this article we will see 4 Ways to Read File with C++ . Among these forms we will have:
- 1. Style C - A classic shape;
- 2. gamefile style - Generally adopted by game developers, in Windows mainly, usually for
.txt
files; - 3. parser style - Many programming languages have this form in their source code;
- 4. POSIX style - For situations where tokens can be created including accented characters.
I’ll show you the example code and it’s up to you to modify it as needed.
Let’s go the list!
1. Style C
As I said at the beginning of the article, this is a classic style, this was the way I used it the most in my life. The first time I posted this code here on the blog was in an article published on November 9, 2019, that is, it’s been a while! :) But, as C and C++ are stable languages, it works so well like in the old days.
It will read all the lines and show it on the screen. This form is often used for general purposes, for example a cat
-like command.
The file is defined at runtime, passed as an argument: argv[1]
.
2. gamefile style
In this form I also chose to use Object Orientation, as it is closer to the real way we usually use it. For this example, use a file.config
similar to the one below:
That is, it is a file that has separate data that will be inserted in any game any, examples: Title, Width, Height, Frames per second and a check for some type of validation.
Note that the code below, reads each data separated by member
of our class. This is the most interesting way when it comes to specific external data.
Data will also be displayed separately according to its member functions. The file is also defined at runtime, passed as an argument: argv[1]
.
Example:
./a.out file.config
3. parser style
Usually programming languages have this style in their source codes. In this example, we are going to tokenize all the characters of an example file JavaScript.
Some observations for this code are:
- If your compiler is Clang/LLVM, you will have after changing the line that has
content.data()
, there is a bug inclang++
, that is, it will only work in GCC/G++. - The code only reads files with a
.js
extension.
Here is an example of some basic JavaScript code that you can use:
C++ code that will read the .js
file:
#4. POSIX style I can say that this style is 100% my authorship. In fact, I’m using it for a programming language that I’m creating, for now it’s still a secret! 😎
It was the way I found to tokenize also accented characters, unlike the other example just above. It does not receive arguments via the command line, but you can change it.
If you want to compile and test it, create a file called file.ter
and put this example(spoiler) inside it:
vim file.ter
This is the code I created, a detail that you may find strange is that the main
function is in a more modern form, that is, some old versions of C++ may not recognize it, if that’s the case, change it for the traditional way.
If you don’t create the
file.ter
file, nothing will be displayed!
That’s all for today, I hope you enjoyed it and we have an appointment in the next article!
Comments