1

So I have a Structures which looks like:

typdef struct Location{
    int x;
    int y;
    char *name;

I need to read some number of structures(The number of structures that I need to read and store can be anything from 2 Structures to 100,000 structures or more) which must be dynamically allocated according to the input and they are read till 'EOF'. The other problem is that the name field in the structure can be of any length so I need to dynamically assign that space for name variable as per the input length.

I know that here I will have to use realloc and use a while loop till EOF. But I have no clue how to structure the Syntax in C to take this input. The following is an example of Standard Input.

[0,0] Lorem ipsum dolor sit adipiscing elit
[5, 0] amet, consectetur 
[10, 0] adipiscing elit
[7, 0] Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium

So the number of this Programming

80TAGGN
  • 11
  • 1
  • use fgets. It has a buffer length parameter [How to read line by line after i read a text into a buffer?](https://stackoverflow.com/q/10560505/995714) – phuclv Dec 01 '19 at 01:02
  • 1
    If you type "realloc grow array" into the search box on this page, you will find lots and lots of questions discussing the basic `realloc` technique. – Steve Summit Dec 01 '19 at 01:22
  • 5
    A good way to read lines where you don't know in advance how long they might be is with the [`getline`](https://linux.die.net/man/3/getline) function, if it's available to you. – Steve Summit Dec 01 '19 at 01:29
  • @SteveSummit Thank you I think getline is the way to go, but how do I extract the integer types in the string that I will receive and store them in the right variable. – 80TAGGN Dec 01 '19 at 01:49
  • 1
    @80TAGGN use `sscanf` – phuclv Dec 01 '19 at 02:13

0 Answers0