Storing text in program.

Hello people!

I have a question that may sound odd and easily bypass-eable but it strikes my curiosity so I will ask it.

My question is the following:

Assume you want to make a program that prompts a series of paragraphs, much like a kindle, you press enter and it displays the next paragraph for instance.

Okay so you might say the easiest and most practical way of doing this is to read from a file. OK.

Now assume you don't want to deal with any file.

How would you go about doing this. Would you store the text in an array of strings, a class, structure...?

Thanks in advance for your feedback and insight.

Regards,

Hugo.
Last edited on
A vector of strings with a string for each paragraph is fine.
this works no matter the source of the data; if you abstract the data from the container, you can later read a text file and handle it the same way, or you can have data hard coded in the program for now and read from a file in the future if you change your mind, keep it flexible.
Sure, you can do that, you would just store it as a series of string literals. This is known as "hard-coding" information into the program. You can do the same thing with any data (pictures, videos), but the process to load the data would be a bit more involved if it isn't just plaintext.

The specifics of what classes/structures you want are up to you, but generally you'd have some sort of array/vector of strings or array of classes that contain strings.

Note that there are limits to how big each string literal can be. MSVC, for example, will allow up to 2048 characters per string literal.
https://stackoverflow.com/a/11488682
Last edited on
Nice, thank you Jonnin for your answer. I see what you mean and it indeed seemed like the best thing to do.

I had that question in mind because for instance if you make a text based program which relied on files to read from, then if you want to send you program to someone for any reason, it's going to be a hassle if the file isn't in the same directory as the cpp file, or if you open the file with a specific path, it's a mess to export it to other users.

Thanks a lot!
Thank you Ganado for your answer. However if you were to do that with - as mentioned, pictures, videos - you would still need to rely on having them stored on your computer and linking them to your program with some external library wouldn't you. Or is there a way to hold the entirety of the media, let's say video, in a program so that you don't need any external dependency to read it?
closed account (E0p9LyTq)
is there a way to hold the entirety of the media, let's say video, in a program so that you don't need any external dependency to read it?

The Windows API allows a program to access read-only data embedded in executable files, the data knows as "resources."
https://en.wikipedia.org/wiki/Resource_%28Windows%29

Do other OSes allow doing this? I don't know.

@FurryGuy
Thanks a lot for the info, I'll look into that!
closed account (E0p9LyTq)
@hoogo,

The more data you add to an executable the more memory it requires so the program can run. Even if the resource is not being used. External resource files used on demand make memory management less of a problem.
Got it yes, it makes perfect sense, thanks!
Do other OSes allow doing this? I don't know.

GCC can do it using the linker to statically embed it.
https://stackoverflow.com/questions/4158900/embedding-resources-in-executable-using-gcc
But you really don't even need to involve the OS. The Windows resource feature is just a major convenience. All you really need is to get some tool which can take data and spit out its hex code as C++ code, and then copy-paste that code into an array. (xxd can do this).

For example, this is the first few bytes of an example PNG file:
constexpr unsigned char image[] = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, /*...*/ };

hoogo,
I see FurryGuy already answered, but just to reiterate: No, you can store the data in your program's source code directly, no need to link files.

That being said, I don't recommend this...
As soon as you grow out of a super simple program, hardcoding all your text and other resources will become a major PITA.

If the problem is transporting your compiled program to another person, send it as a zipped archive.
Last edited on
closed account (E0p9LyTq)
GCC can do it using the linker to statically embed it.

GCC isn't an operating system, it is a compiler. To a compiler data is data, no matter what the data represents.

Data matters to an OS when it can represent icons or images or sounds.

Does *nix -- an operating system -- or MacOS, for instance, allow for embedded data to be used as icons, wave sounds, etc? I don't know.

As I originally said.
I'm... not disagreeing with you. If we want to get pedantic, whatever is embedding the data into a Windows Executable isn't the Operating System itself either, it's MSVC or other Microsoft compiler/linker/tool. I'm just letting OP know that one can use gcc to embed the data, and gcc (or an equivalent port) is available on all 3 major types of operating systems.
Last edited on
you can bury massive amounts of data in a program. Whether you should or not is philosophical, but mechanically, you CAN. A video might be tricky but images, text, and other such is easy enough. While string class may have a limit vector<char> does not. Yea its C-ish but if you need a 2 megabyte vector of text and all you are going to do is print it, that would also work (you can break this into stuff that fits into strings for use upon execution). If you want to get really snarky you can zip the file, embed the binary bytes in your program and unzip it into your datastructure yourself if you get too much to fit in there uncompressed. You can write a C or C++ program that writes the program that embeds the data in a static structure from text or zipped files :P

distribution of a program shouldn't even begin to be your concern.
small toys sent to buddies can be distributed with a zip file of all the stuff you need. Serious programs will use an installer but the result is the same and you end up with the files you need where you need them either way.

programs do have a limit on how much static data you can jack into them. Its many megabytes uncompressed, I forget how many the current OS flavors will take but its more than you probably need for this... if you are pushing those limits, you need to use a file (well, you really should use a file anyway, but its your project..). A few times of having to recompile and redistribute the thing because of typo fixes should be convincing...
Last edited on
The std::string class's limit is going to be whatever your computer can allocate, just like std::vector, but what you said is true. (C++11 also made it standard that both shall be logically contiguous in memory.)

small toys sent to buddies can be distributed with a zip file of all the stuff you need
+1
Last edited on
Topic archived. No new replies allowed.