How to take input from .txt file and store in different variables

Hi all, I am new in C++ programming. I am building a program where I have to take input for many variables. Some variables are single integers and some are array of integers. A sample input text file is like this:
4 /* Number of rows, I want to store in int variable "N" */

5 /* Number of columns, I want to store in variable "X" */

20 40 20 25 30

20 30 40 20 25

20 40 20 25 30

20 30 40 20 25 /* array of N rows and X column , i want to store in A(N,X) */

can somebody help me out?

Thanks
declare the array like this:

int arr**;
The new operator will be used here
http://www.cplusplus.com/doc/tutorial/dynamic/

When you read in the first number, that will be the number of rows, hence the number of arrays you will need. So you read in 4, then you do:

arr = new int *[4];//Pointer to 4 spaces in memory or 4 arrays of undetermined sizes.

1
2
//First for-loop starts here:
for (int x = 0; x < 4; ++x)


Next you will read the 5, this will be your number of columns hence number of items to be contained in each array. So the next line after the for-loop should be:

arr[x] = new int[5];//Now you have given that array a size and now you can start storing the values in it.

1
2
//Second for-loop starts here
for(int i = 0; i < 5; ++i)

Now to store values in the arrays you have created, you do:

1
2
arr[x][i] = 20
arr[x][i+1] = 40


and so on...
Last edited on
Thanks Smac89, this is helpful.

However, my data is in text file. So how i will read my data from text file. I have read input/output document. I was trying to use getline command, but the problem is how to store the data from text file into my variables.

For eg. i have to read that number of rows are 4 and then store the value in variable "N" so that i can define

arr = new int *[N]; instead of arr = new int *[4];

actually 4 is just a number , it can be any integer.

Thanks again!
Reading in the values from the text file is as easy as reading it in from a user. There are 3 ways:

Using input/output redirection <, having program open and read from a file using ifstream(c++) or FILE* (c), or using the text file as arguement when running the program(you've probably seen int main(int argc, char **argv)) argc is number of arguements, argv is an array of arguements

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int row, col, **arr;
    
    ifstream infile;
    infile.open("store.txt");
    
    if (infile.good())
    {
        infile >> row >> col;
        
        arr = new int*[row];
        for (int g = 0; g < row; ++g)
        {
            arr[g] = new int[col];
            for (int f = 0; f < col; ++f)
            {
                infile >> arr[g][f];
                cout << arr[g][f] << " ";
            }
            cout << endl;
        }
        delete [] arr;
    }
	return 0;
}


//Normal Or input redirection
#include <iostream>

using namespace std;

int main()
{
    int row, col, **arr;
    
    cin >> row >> col;
    
    arr = new int*[row];
    for (int g = 0; g < row; ++g)
    {
        arr[g] = new int[col];
        for (int f = 0; f < col; ++f)
        {
            cin >> arr[g][f];
            cout << arr[g][f] << " ";
        }
        cout << endl;
    }
    delete [] arr;
	return 0;
}


//command-line arguement
#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char **argv)
{
    int row, col, **arr;
    FILE* input;
    
    if (argc > 1)
    {
        input = fopen(argv[1], "r+");
        
        if (fscanf(input, "%d %d", &row, &col) == 2)
        {
            arr = new int*[row];
            
            for (int g = 0; g < row; ++g)
            {
                arr[g] = new int[col];
                for (int f = 0; f < col; ++f)
                {
                    fscanf(input, "%d", &arr[g][f]);
                    cout << arr[g][f] << " ";
                }
                cout << endl;
            }
            delete [] arr;
        }
    }
    else
        cout << "File should be run like " << *argv << " filename \n";
    
	return 0;
}


This is what file looks like:
4

5

20 40 20 25 30

20 30 40 20 25

20 40 20 25 30

20 30 40 20 25
Last edited on
Topic archived. No new replies allowed.