2D character arrays I/O

Hi all,

I have a bit of an issue that I cannot seem to find the answer to, but i apologise if it is obvious and I am just missing something.

I am making a code breaking program, which basically loops through possibilitys for the key, decrypts the code using the current key, checks to see if the output of the decrypter contains dictionary words, and if there are several it shows the user that.

My issue is that string class doesn't seem to work very well with the decryption, so I must use char arrays in the form char word [letter or word][line of dictionary] or vice versa.

Normally, I would do something like this:

1
2
char word [20];
cin.getline(word, 20);


That works fine, but how would I do that within a 2d array? Basically I want to read a list of words from a file [235887] of them (think of that as rows) and then have max [40] columns.

I would like to do something like this:

1
2
3
4
5
6
7
8
char word[40][235887];

int i = 0;
while (i <= 235997)
{
cin >> word[][i]; // cin or cin.getline, either would be fine.
i++;
}


Here is some of my actual code... I used string to read it in but I still have to get it back into char format before I can compare it to my decrypter.

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
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;

string word [235887];
char charword [40][235887];

void DictLoad(void)
{
    cout << "Importing dictionary from file...";
    long int i =0;
    ifstream fin;
    fin.open("dict.txt");

    while (i <=235887)
    {
        getline(fin,word[i]);
        i++;
    }
    fin.close();
    cout << "Complete!";

    i=0;
    while (i <=235887)
   {
        //assign each string to char array (2d)
   }
}


The words do import like this, but now I need to get it back into null terminated character arrays... (char)

Either skipping the string and going directly to char from file or converting the string to char[][stringnumber] would be great.

Sorry if that was a bit long, any help would be greatly appreciated.
Thanks
Ryan
Last edited on
Topic archived. No new replies allowed.