question...2d array to txt

Pages: 12
How to save 2d array to txt file?
I wrote this in a hurry, so it could have some small errors, but I doubt it does. Anyways, this is how you do it.

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
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main(int argc, char *argv[])
{
    // Method I:
    ofstream out("file.txt");
    
    int matrix[101][101];
    int i, j, Nrow, Ncol;
    
    cin >> Nrow >> Ncol;
    
    for (i = 1; i <= Nrow; i++)
        for (j = 1; j <= Ncol; j++)
            cin >> matrix[i][j];
    
    for (i = 1; i <= Nrow; i++)
    {
        for (j = 1; j <= Ncol; j++)
            out << "matrix[" << i << "][" << j << "] = " << matrix[i][j];
        out << "\n";
    }
    out.close();
    
    // Method II:
    ofstream Out("File.txt");
    
    int nRow, nCol, t;
    vector< vector<int> > Matrix;
    
    cin >> nRow >> nCol;
    
    for (i = 1; i <= nRow; i++) 
    {
        Matrix.push_back(vector<int>());
        
        for (j = 1; j <= nCol; j++)
        {
            cin >> t;
            Matrix[i].push_back(t);
        }
    }
    
    vector< vector<int> >::iterator row_it = Matrix.begin();
    vector< vector<int> >::iterator row_end = Matrix.end();
    
    for ( ; row_it != row_end; ++row_it )
    {
        
        vector<int>::iterator col_it = row_it->begin();
        vector<int>::iterator col_end = row_it->end();
        
        for ( ; col_it != col_end; ++col_it )
                Out << *col_it;
        Out << '\n'
    }
    
    Out.close();
    
    return 0;
}


Best of wishes,
~ Raul ~
Last edited on
If i have in 2d string array named X[14][5] some values that user write.
How to that X save to txt with all values?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <iomanip>

int main(int argc, char *argv[])
{
    char X[14][5];
    int i, j;
    
    for (int i = 0; i < 14; i++)
        for (int j = 0; j < 5; j++)
            cin >> X[i][j];
            
    ofstream out("file.txt");  // file.txt can be any name that your file might have
    
    for (int i = 0; i < 14; i++)
    {
        for (int j = 0; j < 5; j++)
            out << setw(5) << X[i][j]; // setw(n) -- n means that your string will have a field of 'n' spaces reserved
        out << "\n";
    }
    
    return 0;
}


EDIT:

Example of data for the string matrix:

1
2
char *x[2][3] = {{"Apple", "Samsung","LG"},
                 {"Google", "Microsoft", "IBM"}};


Best of wishes,
~ Raul ~
Last edited on
Here's another option: I like to save 2d arrays as a CSV (comma seperated value) file. It allows spreadsheet applications like Excel to open the file easily.
1
2
3
4
5
6
7
8
9
10
11
12
char* X[14][5];

// Populate X[][] here...  Then do the following to write it to a csv file

ofstream out("file.csv");

for (int i = 0; i < 14; ++i)
{
    for (int j = 0; j < 5; ++j)
        out << X[i][j] << ',';
    out << endl;
}
Ok...thank you...How to delete all values of this array?...I try delete[] X but it not work
If you want to delete it, you need to use <vector> and <algorithm> libraries:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    // code
    
    vector< vector<char> > Array;
    
    // code
    
    Array.erase(Array.begin(),Array.end());
}


EDIT: That implies using the 2nd method I showed

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
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main(int argc, char *argv[])
{
    // Method II:
    ofstream Out("File.txt");
    
    int nRow, nCol, t;
    vector< vector<char> > Matrix;
    
    cin >> nRow >> nCol;
    
    for (i = 1; i <= nRow; i++) 
    {
        Matrix.push_back(vector<char>());
        
        for (j = 1; j <= nCol; j++)
        {
            cin >> t;
            Matrix[i].push_back(t);
        }
    }
    
    vector< vector<char> >::iterator row_it = Matrix.begin();
    vector< vector<char> >::iterator row_end = Matrix.end();
    
    for ( ; row_it != row_end; ++row_it )
    {
        
        vector<char>::iterator col_it = row_it->begin();
        vector<char>::iterator col_end = row_it->end();
        
        for ( ; col_it != col_end; ++col_it )
                Out << *col_it;
        Out << '\n'
    }
    
    Out.close();
    
    return 0;
}


Best of wishes,
~ Raul ~
Last edited on
I dont know anithing about vectors... Is there something easy to delete all 2d array?
Last edited on
if i do this....program stops...and write error
for (int i = 0; i < 14; ++i)
delete[] X[i];
Sorry, give me a few min to think about it.

Question: Do you want to delete the 2D array, and never use it again ?

What confuses me, is the fact that I really don't know why do you need to delete it. If you need to use the 2D array again after printing it to file that is easy. If you want to delete is permanently and never use it again, then the delete is not needed as the function main has its own destructors which are being called at the end of the function.

If you want me to help you, please be more specific and tell me what exactly is your program about/what is it supposed to do/why do you find it necessary to delete the array/anything else that you can tell me so that I can understand what you are looking for.
Last edited on
Yes, I want to use this 2d array after deleting...
This is from my code...
User write something to that array and if user delete array then i want to go back to original state...

string X[14][5]= { {"Sati "," Ponedjeljak"," Utorak"," Srijeda", " Cetvrtak"},
{"7-8 "},
{"8-9 "},
{"9-10 "},
{"10-11"},
{"11-12"},
{"12-13"},
{"13-14"},
{"14-15"},
{"15-16"},
{"16-17"},
{"17-18"},
{"18-19"},
{"19-20"}};
This is what my code is for...this link is my printscreen image
http://www.freeimagehosting.net/8puoq

From linked list i move values to 2d array. And if user want to delete array then i need this code to delete it. I have deleted linked list but values stay in 2d array...When user delete it i want to go back to previus stage...On screen would be only days and hours
Last edited on
I remember your code now. You were doing a daily-school schedule.
I don't have a lot of experience in working with strings.

If I am right, you could do that by:

1
2
3
4
for (int i = 2; i <= 14; i++)
    for (int j = 1; j <= 5; j++)
        X[i][j] = '\0';
    
Yes :)...I make daily-school schedule :) and it work...
I said i = 2 and j = 1 because your 2D array has 14 lines and 6 columns

"Hours", "Sati "," Ponedjeljak"," Utorak"," Srijeda", " Cetvrtak"
----||----------||---------------||------------||----------||-------------||
---0,0--------0,1-------------0,2----------0,3--------0,4-----------0,5
---1,0
---1,1
---1,2
---1,3
---....
---1,13


LINES = 13 + 1 = 14
COLUMNS = 5 + 1 = 6

What I meant is that "Hours" is [0][0], "Monday" is [0][1], ..., "Sunday" is [0][5].

EDIT: Congratulations, and good luck with it. :)
Last edited on
Thank you :)
I put this code and not work :(
Program crashes

for (int i = 2; i <= 14; i++){
for (int j = 1; j <= 5; j++)
X[i][j] = '\0';}
Hmm :/

I really don't know why it crashes. :(
I'm sorry but I can't help you more with this because I didn't work with strings/characters before ( only for fun -- simple exercises ).

I hope someone can help you fix it and everything will run correctly as soon as possible.

Good luck. ^^
Last edited on
Ok...thanks for help :)
No problem. ;)
See you around soon. :P
Hmm, though... I think this could work. Just try it too:

1
2
3
char *begin = &X;
char *end = begin + sizeof(X);
fill(begin, end, 0);


PS: Not sure if that would work, or if it will delete all the vector or just the part with the school subjects. :/
Last edited on
Pages: 12