Storing text file in 2D array

I want to store a text file in 2D array. I stored it in a 1D array but don't know how to do so in a 2D array.
following is the data set.

T N E M E C N U O N N A G R S
E E L G G R O O M S L I E V O
C E Y E N O M O S L E S N S C
G U D U A A F E C A P A T S I
N Q A C H U R C H O E R L A E
U W L O C D E R N G A R E L T
O E O U X N S S A N S R M C Y
Y Y L R E O I S S G A U A Y G
R T T T C B T A R H N R N A I
D U I G I N C M S I T I T S F
O D N L E T O A O N T H K R T
W I I R I T L N O S E E H O S
R T A O S B A C E R E M O N Y
Y P N U U L O D O N A T I O N
I S C M P P D N A B S U H H S

Album, Announcement, Arrange, Bond, Ceremony, Church, Class, Contract, Court, Crown, Custom, Destiny, Donation, Dowry, Duty, Exchange, Gather, Gentleman, Gifts, Glee, Goals, Groom, Honor, Husband, King, Lady, Money, Nobility, Pace, Parents, Peasant, Plan, Queen, Reason, Responsibility, Ring, Rite, Seal, Serf, Share, Society, Title, Transactions, Union, Veil, Young.

the program I wrote to store it in 1D array is as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
    char buff[500];
    ifstream fin ("E:\\hw3\\sample puzzles\\teleword_1.txt");
    if (fin.fail())
    {
        cout<< "Couldn't open File";
    }
    else
    {
        while (fin.getline(buff,500))
        {
            cout<<buff<<endl;
        } //end of nested while 1


    } //End of main else
    return 0;
}


just don't know how to store it in a 2D array. Any help would be appreciable.
Ps: I want to store it in a character 2d array.
Last edited on
Edit: I wasn't happy with it. Too much trying to be clever and change the original code only a tiny amount.
Last edited on
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cctype> // toupper
using namespace std;

const int Size = 15;  // is this size fixed or can it change?

int main() {
    ifstream fin("wordpuzzle.txt");
    if (!fin) {
        cout << "Can't open word puzzle file\n";
        return 1;
    }

    char grid[Size][Size];
    for (int row = 0; row < Size; row++) {
        char ch;
        for (int col = 0; col < Size && fin >> ch; col++)
            grid[row][col] = ch;
    }
    
    vector<string> words;
    string word;
    for (int row = 0; fin >> word; row++) {
        char ch = word[word.size()-1];
        if (ch == ',' || ch == '.')
            word.resize(word.size() - 1);
        for (auto& x: word)
            x = toupper(x);
        words.push_back(word);
    }

    for (int r = 0; r < Size; r++) {
        for (int c = 0; c < Size; c++)
            cout << grid[r][c] << ' ';
        cout << '\n';
    }
    for (const auto& x: words)
        cout << x << '\n';
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
   string line;
   vector<vector<char>> grid;
   ifstream fin( "teleword.txt" );
   while( getline( fin, line ) )
   {
      line.erase( remove( line.begin(), line.end(), ' ' ), line.end() );
      grid.push_back( vector<char>( line.begin(), line.end() ) );
   }

   for ( auto row : grid )
   {
      for ( auto c : row ) cout << c;
      cout << '\n';
   }
}


TNEMECNUONNAGRS
EELGGROOMSLIEVO
CEYENOMOSLESNSC
GUDUAAFECAPATSI
NQACHURCHOERLAE
UWLOCDERNGARELT
OEOUXNSSANSRMCY
YYLREOISSGAUAYG
RTTTCBTARHNRNAI
DUIGINCMSITITSF
ODNLETOAONTHKRT
WIIRITLNOSEEHOS
RTAOSBACEREMONY
YPNUULODONATION
ISCMPPDNABSUHHS
@lastchance as I said I wanted to store them in a character array.
Atbq Thanks for the assistance
but your program is not ending....I guess it has stuck somewhere in loops. Can you please look for that?
Last edited on
Naveed Khan wrote:
I wanted to store them in a character array.

What do you think
vector<vector<char>> grid;
is?

If you want the letter in the ith row and jth column ... this is now grid[i][j].


Naveed Khan wrote:
Atbq

RTBA
Last edited on
Naveed Khan wrote:
but your program is not ending....I guess it has stuck somewhere in loops. Can you please look for that?


Whose program are you referring to?


Input file CALLED teleword.txt AND IN THE CURRENT WORKING DIRECTORY:
T N E M E C N U O N N A G R S
E E L G G R O O M S L I E V O
C E Y E N O M O S L E S N S C
G U D U A A F E C A P A T S I
N Q A C H U R C H O E R L A E
U W L O C D E R N G A R E L T
O E O U X N S S A N S R M C Y
Y Y L R E O I S S G A U A Y G
R T T T C B T A R H N R N A I
D U I G I N C M S I T I T S F
O D N L E T O A O N T H K R T
W I I R I T L N O S E E H O S
R T A O S B A C E R E M O N Y
Y P N U U L O D O N A T I O N
I S C M P P D N A B S U H H S


Program (with assert statement added):
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;

int main()
{
   string line;
   vector<vector<char>> grid;
   ifstream fin( "teleword.txt" );     assert( fin );
   while( getline( fin, line ) )
   {
      line.erase( remove( line.begin(), line.end(), ' ' ), line.end() );
      grid.push_back( vector<char>( line.begin(), line.end() ) );
   }

   for ( auto row : grid )
   {
      for ( auto c : row ) cout << c;
      cout << '\n';
   }
}


Output (to console, purely for checking):
TNEMECNUONNAGRS
EELGGROOMSLIEVO
CEYENOMOSLESNSC
GUDUAAFECAPATSI
NQACHURCHOERLAE
UWLOCDERNGARELT
OEOUXNSSANSRMCY
YYLREOISSGAUAYG
RTTTCBTARHNRNAI
DUIGINCMSITITSF
ODNLETOAONTHKRT
WIIRITLNOSEEHOS
RTAOSBACEREMONY
YPNUULODONATION
ISCMPPDNABSUHHS


It ends perfectly well ... output above ... and is NOT stuck in loops.
Last edited on
Topic archived. No new replies allowed.