Home c C: how and where to store large texts

C: how and where to store large texts

Author

Date

Category

There is a program that gives the user a random text. In the role of storing these large texts, I chose an array, for example:

char * en_arr [10];

Then, starting from 0, the filling goes:

en_arr [0] = "Now indulgence dissimilar for his thoroughly has terminated. Agreement \ n \
offending commanded my an. Change wholly say why eldest period. Are \ n \
projection put celebrated particular unreserved joy unsatiable its. In then \ n \
dare good am rose bred or. On am in nearer square wanted. Silent sir say \ n \
... ";
 en_arr [1] = L "Warmly little before cousin sussex entire men set. Blessing it ladyship \ n \
on sensible judgment settling outweigh. ... ";

Then comes srand () + rand () and the function returns the text under the random index of the array, it turns out that the returned text is random. I think this solution is a bit ugly, because the file texts.c is full only of these long texts. It would probably be more logical to store these large texts in a regular text file and release the reading of this file into the program buffer. But I don’t understand a little how to do it. If I use the read () system call, I get the entire contents of the file, which is logical, but I would like to extract the specific text

For example a text file consists of:

Huge text one. Huge text one. Huge text one.
Huge text one. Huge text one. Huge text one.
Huge text two. Huge text two. Huge text two.
Huge text two. Huge text two. Huge text two.
Huge text three. ...

An empty line between the texts to separate the texts. The program generated the number 1. How to read only the first text into the buffer? Or, for example, the number 3 was generated, which means we read 3 text into the buffer, without affecting other texts. Or is there some other simpler solution?

I remember that in Python I once had to work with yml files and there it could be done in this way:

1:
 text
2:
 text

Then generate a random number, refer to this file, and get this text by the text index. And in C, you will probably have to come up with something different …


Answer 1, authority 100%

You can also do this!

/ * main.c * /
#include & lt; stdio.h & gt;
int main (void)
{
  char * text [] =
  {
    #include "text.h"
  };
  printf ("% s", text [2]);
}
/ * text.h * /
"Now indulgence dissimilar for his thoroughly has terminated. Agreement \ n \
offending commanded my an. Change wholly say why eldest period. Are \ n \
projection put celebrated particular unreserved joy unsatiable its. In then \ n \
dare good am rose bred or. On am in nearer square wanted. Silent sir say \ n \
... ",
"Warmly little before cousin sussex entire men set. Blessing it ladyship \ n",
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ac tempor risus, ut \ n \
feugiat lectus. Maecenas hendrerit metus et velit molestie, pretium mollis libero \ n \
accumsan. Praesent pretium lorem ut nisi rutrum rhoncus. Cras ipsum libero, convallis vel \ n \
lorem luctus, tempor bibendum turpis. Duis mauris metus, scelerisque sit amet lorem non, \ n \
venenatis placerat nisi. Praesent sit amet tellus non augue euismod ornare. Etiam id ex \ n \
ultricies, malesuada elit at, pharetra quam. Vivamus efficitur, ex eget blandit aliquet, lorem \ n \
orci vulputate dolor, vel iaculis sem lorem ac lorem. In a malesuada ligula. Aliquam quam mi, feugiat \ n \
vel mattis at, rhoncus ut massa. Curabitur aliquam rutrum nulla ac bibendum. Donec vitae nunc \ n \
rutrum, dapibus diam sed, pellentesque lectus. Etiam id risus suscipit, euismod purus a, dignissim enim. \ N \
Nullam pharetra pellentesque dignissim. Phasellus nibh est, vestibulum vitae fermentum quis, lobortis \ n \
sollicitudin dui. Vestibulum a libero lacinia metus blandit porta vel et nunc. \ N "

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions