How to copy strings from file into array of pointers

I am trying to copy string Line BY Line from text file into array of pointers. lets say file has only two lines for example.

This is an apple.
This is another apple.

I want to copy both of these lines from file to array of pointers. Below is the code which i am trying to use for this. I know it has many mistakes please assist me. Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <string.h>

using namespace std;

int main()
{
    char const *filePath = "test.txt";

    char *getLines[2];
    char chPerLine[20];
    char *ptrString;
    fstream myFile;
    myFile.open(filePath, ios::in | ios::out);
    myFile.getline(chPerLine, 20, '\n');
    ptrString = chPerLine;
    strcpy(*getLines, ptrString);
    *(ptrString + 1);

    myFile.getline(chPerLine, 20, '\n');
    ptrString = chPerLine;
    strcpy(*getLines, ptrString);
    myFile.close();

    cout << getLines[0] << getLines[1];
    return 0;
}
First, let's go line-by-line to see what is wrong:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <string.h>

using namespace std;

int main()
{
    char const *filePath = "test.txt";

    char *getLines[2];
    char chPerLine[20];
    char *ptrString;
    fstream myFile;
    myFile.open(filePath, ios::in | ios::out);
    myFile.getline(chPerLine, 20, '\n');

    // You don't need this assignment here, because an array can be implicitly
    // converted to a pointer.
    ptrString = chPerLine;
    
    // Here you are trying to copy to your array, but you first need to
    // initialize the pointers to either a statically or dynamically allocated
    // array.
    strcpy(*getLines, ptrString);
    // This statement doesn't actually do anything, since the value of the
    // expression isn't assigned to anything.
    *(ptrString + 1);

    myFile.getline(chPerLine, 20, '\n');
    ptrString = chPerLine;
    strcpy(*getLines, ptrString);
    myFile.close();

    cout << getLines[0] << getLines[1];
    return 0;
}


Now my suggestion is to use a loop to perform your logic. If you are unfamiliar with loops, you should read the C++ tutorial on this site. Here's some pseudocode to get you started:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
static const int nStrings = 2;

int main()
{
    const char *filePath = "test.txt";
    char *getLines[2];

    fstream myFile;
    myFile.open(filePath, ios::in | ios::out);

    for int i = 0; i < nStrings; ++i)
    {
        // allocate a new c-string
        // read in to the new c-string
        // assign the appropriate pointer in the array to the new string
        // print the string.
    }

    return 0;
}


The advantage of this is that it will allow you to easily expand your program to read more lines, simply by changing the value of nStrings. This isn't the most error-proof solution, but it improves upon what you had.
Topic archived. No new replies allowed.