How to properly store an array of constant data? [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
My question is particularly pointed towards what's neat and efficient and won't get me made fun of as a programmer.
So basically I want to store a huge file of names of PNGS. So of course I'd use an array of strings in order to store all those names. But my question is should I store these strings in something like a binary file? Or should I just put them straight onto the script? I personally hate the idea of using a text file to store them but what do I know.
If I were to put them on a script then it would look something like this:
const std::string tileFile[textureAmount] ={
"Grass.png",
"Dirt.png"
};
But this just looks really stupid to me for some reason. How would one handle a situation like this more professionally?

A character is 1 byte, whether you write a text file or a binary file. The only savings would be if you did some encoding or compression to it, and text does compress nicely since not all the bits in the chars are usually used.
But one pragmatic rule for programming is, "remember the power of plain text". Binary is nice, but if you don't need it, don't use it. Text is easier to work with, to read, to understand when it is corrupted, to use with other tools, and so on. If you can map the data into memory, use binary. If you're going to gzip it or something like that, use binary. If you're just storing the data and it's not excessively big, then I'd recommend a nice, easily parsed text file.
And don't feel ashamed of it. :)

Related

Flatbuffers usage [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I’m kind of confused how flat buffers work.
I tried looking at the docs but am still confused.
Basically, what I need to do is
Load JSON data (or actually, ANY data format as long as it’s changeable by another user and readable) from a file into a struct or read the fields one by one.
Save the struct back into any readable data format as a file once the app closes
That’s why I’m confused on flatbuffers.
How do you change the file once it’s saved? Is the saved result binary? Or is that not what it was built for?
I’m using RapidJson currently.
The usage is read text data into a struct, when app ends save struct into text that’s modifiable.
Flatbuffers are a compact binary representation of a given data structure, with the promise that it can be used "straight from the wire", without any deserialization after the fact. By contrast, protocol buffers fill the same niche but need to be (de)serialized.
For your purposes, just stick with JSON or YAML, since "readable by humans" is a priority.

How to load and parse the content of a text file into diferent variables [c++] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i'm learning the basics of c++.
also at the same time i try to aplly what i've learned into a project.
i'm trying to make a GUI program for a command-line program.
the purpose of that program is to interact with PS2 Hard Drives, and i want to automate almost every feature it has.
the first thing i'm trying to do is parse a text file dumped by the program:
the text file contains a list of elements.
every element will contain this :
"media" "size" "Region Code" "name of the game"
Also, at the end of the file the total,used and free space of the HDD is written:
total 305152MB, used 199296MB, available 105856MB
I need to store into arrays (or vectors) the data of every game and the HDD space
Here's an example of what the text file looks like
https://mega.nz/file/o85SmLBZ#qf8FCXByI5-icz9LlSNFGiL7JXFArpwxODDLNrQ1LPc
Whatever I write to handle this will be executed several times (after erasing or installing games)
If you want to read and write to and from files, I suppose your best bet is the fstream library, which well... is used specifically to do that.
Here is a simple tutorial for the most fundamental ways to use the library.
Using this, you will read the file. There are examples for reading values one-by-one, but since the last line is specific, you might want to look into getline which shows up in that tutorial too.
Parsing it is the logic of your program, so I guess the first thing you want to do is put the values you read into arrays of some sort.

How are files stored in memory and how can i use that knowledge in C++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How are files stored in memory?
I suppose that when i write in a text file something like Hello World, the file in memory looks like this:
01001000 01100101 01101100 01101100 01101111 1 Byte 1 Char?
Well... I'm 90% sure I'm wrong about this and that's why I put this question.
The actual thing is that I really want to know how Images are stored into memory because I want to use this to edit images or create ASCII art, and i feel like i could do this without a 3rd party library.
The reason I didn't put my time into learning a library is that there are lots of them(I don't know which one to pick) and i don't know at what point I should looking into them... Well, this would be a different question
Files are stored on media such as disk, they are represented in memory. How data is stored on media will depend on the file content, in your case the type of image, such as jpeg, png, etc. You can look those up online and then write code which reads that data into whatever representation in memory is best for you.
Many formats are compressed and will require complex work to decode, so you may want to start by looking at bmp. However, even this is still "encoded" and if you can't work with the data represented that way, you'll need to choose your representation, e.g. a giant 2d array of RGB values, and write your own code to read the disk image into that representation.

Is my company doing this right, sharing data between exes? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
First off, my company is into power grid, not IT, so software is kinda a secondary role here.
I work on a power system simulation software, very old code base in C++ with MFC, like 15 years old. What we do is take large amounts of data, ~100,000 floating point values then format and write to a text file (most of the code actually uses the C FILE structure to do this). Then, it's read by a separate engine exe which computes the electrical algorithm (Electrical algorithms are mostly numeric solutions of system of diffn equations) and then writes back huge amount of data to another text file, which we read and update the UI.
My question is, is this how it should be done? It there a way to skip writing into the text file and directly pass the data to the exes?
exes are called using CreateProcess() MFC function.
EDIT::
Sorry, site won't let me comment.
#Vlad Feinstein Well, yes, it's like a Ladder. A thing called load flow solves power flow through the lines, which in turn will be used to find stability of the systems, which in turn for overvoltage ect. It's huge, the UI is million+ lines of code, engine exes another million maybe.
Doesn't MFC already implement IPC using Dynamic Data Exchange? I can pass strings to another process's PreTranslateMessage() func. A scaled up version of that?
There is no such a thing as "should be done as ..." there are multiple methods to do IPC and while the method you describe might not be the fastest, it is a viable solution nevertheless. If the performance doesn't bother you in this particular case you should not bother with modifying it. It is exactly the case where the phrase "if it ain't broke, don't fix it" applies.
Probably, you would not want to make any new IPC in the application that way, though.

How to approach a C++ parser [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am wanting to have a go at a C++ parser for a formatter I am making.
You can obviously open a file and use getline(..) or get(), is this reasonable way of starting things off and then working out a system using vector arrays and hence creating loads of arrays and somehow structuring out and processing what you are doing from there. For example say I wanted to find ever function in a source file, all functions have the common syntax, "(){" once whitespace has been removed, so do you just look for common delimeters to parse out the sections into arrays. I suppose I will learn as I go.
Or I also assume there are tried and tested ways of doing this, and I would likley just be reinventing the wheel as they say.
C++ is a language that is quite hard to parse in the first place. So if you want anything other that really trivial C++ code to be "understood" by your parser, you are definitely better off starting with an existing product.
The Clang frontend library would perhaps be a good starting point.
There are also a number of "source to source" conversion examples based on clang. Here's one of them: http://eli.thegreenplace.net/2012/06/08/basic-source-to-source-transformation-with-clang/