Creating a jagged array using a txt file?

I wanted to make a C++ code where, a txt file has a number in the beginning with the amount of rows in the column, and then there would be data in the bottom that a dynamic 2d array will take. The thing is, I wanted to make a jagged array, but not specify the exact length for each row. So far, I've got the rest of the code done, but I can't figure out this part of the logic. Help would be greatly appreciated.

Here's the code I got so far btw:
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 #include <iostream>
#include <fstream>
using namespace std;

void Readfile(int**, ifstream&);

int main()
{
	int** Matrix = {};
	int Rows = {};
	char FileName[100];
	ifstream Main;

	cout << "Enter File Name here: ";
	cin >> FileName; // This will allow a user to enter their own txt file for input

	for (int i = 0; i < 100; i++)
	{
		if (FileName[i] == -52 || FileName[i] == '\0')
		{
			for (int j = i; j < (i + 4); j++)
			{
				if (j == i)
				{
					FileName[j] = '.';
				}
				else if (j == i + 1 || j == i + 3)
				{
					FileName[j] = 't';
					if (j == i + 3)
					{
						FileName[j + 1] = '\0';
					}
				}
				else if (j == i + 2)
				{
					FileName[j] = 'x';
				}

			}
			break;

		}
	}

	Main.open(FileName);

	if (Main.is_open())
	{
		cout << "This file exists!" << endl;
		cout << endl;
		Readfile(Matrix, Main);
	}
	else if (!Main.is_open())
	{
		cout << "This file does not exist." << endl;
	}
}

void Readfile(int** Matrix, ifstream& Main) // Function for reading data from the txt file
{
	int Rows;
	Matrix = new int*[];
	Main >> Rows;
	int *Column = new int[Rows];


	for (int i = 0; i < Rows; i++)
	{
		
		Matrix[i] = new int[Column[]]; // With an input 'i' this would be Matrix[i] = new int[Column[i]];
		// because the columns will be decided by the txt file the 'i' for Column[] is omitted.

		for (int j = 0; j < Column[i]; j++)
		{
			Main >> Matrix[i][j]; 
		}
	}
	

}
Just use a vector<vector<int>>

Specify the format of your input file. (e.g. by giving an example).
With a test data file as follows (no need to specify the number of rows):

data.txt
1 2 3
4 5 6 12
7 9

Using a 2D std::vector to store the data there is no need to manually manage the memory:
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdlib>

void ReadFile(std::vector<std::vector<int>>& vec, std::ifstream& file);

int main()
{
   std::string   fileName { "data.txt" };  // let's pretend the user gave a filename
   std::ifstream file(fileName);

   if (!file)
   {
      std::cout << "Ooops! That file doesn't exist!\n";
      return EXIT_FAILURE;
   }

   std::vector<std::vector<int>> matrix;

   ReadFile(matrix, file);

      for (const auto& row : matrix)
   {
      for (const auto& col : row)
      {
         std::cout << col << ' ';
      }
      std::cout << '\n';
   }
}

void ReadFile(std::vector<std::vector<int>>& vec, std::ifstream& file)
{
   std::string input;

   while (std::getline(file, input))
   {
      std::vector<int> temp;
      int               item { };

      std::stringstream ss(input);

      while (ss >> item)
      {
         temp.push_back(item);
      }

      vec.push_back(temp);
   }
}
Topic archived. No new replies allowed.