Reading text into an array

Hi, I am new to the site and and finding it a great source of information.I would be very grateful if someone could help me with a small problem. I am trying to write a simple program that will read text file containing two columns. The first columns contains individual words in the (Catalan) language, the second column contains the English meaning of the word. I am trying to create a simple program that can randomly choose a word from my growing list of Catalan words, and ask me what the meaning of the word is. If I guess correctly it chooses another and so on.My plan is to make it do this at random intervals through the day while I am working at my computer. The problem is, although I have been writing algorithms for mathematical applications in C++, I have no experience of working with text, characters or strings. The text file would be as follows

word1 meaning1
word2 meaning2
word3 meaning3

I cannot get the coded to read the words into an array
eg. char myarray[4][2];
Ideally I would like to read in the whole text file (which will grow as my vocabulary increases, so the size will file will increase. The code, I have been trying to use is as follows (borrowed from advice in this forum). This is my first attempt, it does not manage to read in the words and is very rustic I am sure. Perhaps someone could advise me if there is a better approach. I am sure learning to do this will help my research work also. Just any answers to my immeadiate problem would be fine. Any futher advice on how to approach the wider aim is also welcome. Many thanks
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
[#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{

    // Array to read into (at most 100 characters + ASCIIZ)
    char   two_dim_array[ 4][2 ];     // [ Rows ][ Cols ]
    size_t row = 0;
    size_t col = 0;


    size_t   chars_read = 0;   // Number of characters read so far (used to index array)
    ifstream input( "catalan.txt" );  //  The input file

// Check 2 things: 1) file is still readable, and 2) I'm not going to overflow my array.
    while ( input && chars_read < 8 )
    {

        input >> two_dim_array[ row ][ col ];
        if ( input )
        {
            if ( ++col == 1 )        // Read all  columns in this row yet?
            {
                ++row;                  // Move to next row
                col = 0;                //  And don't forget to reset column!
            }
        }

        if ( input )                              // If I read successfully...
            ++chars_read;                  //     then increment # of characters read 
    }

    cout << "the word is: "<<two_dim_array[1][1];//just an example
    return 0;]



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

int main(){
	string data_array[3][2]; //You can make it dynamic to handle as much as the file has
	ifstream in_file("data.txt", ios::binary);
	//Check if the file is open
	if(!in_file.is_open()){
		cout << "File not opened..." << endl;
		return 1;
	}
	//read the file (two columns) to the array
	for(int i=0; !in_file.eof(); i++){
		in_file >> data_array[i][0];
		in_file >> data_array[i][1];
	}
	
	//Display the array
	for(int i=0; i<3; i++){
		cout << data_array[i][0] << ", " << data_array[i][1] << '\n';
	}
	return 0;

}


This program reads the two columns from the file into the data_array.
It is better to use a string type array because you dont have to worry about the size of the reading. If you use char array then you need 3 dimensional to handle: 1. The line number, 2.The Row, 3.The length of the chars...
Also at your program at line 27 the if statement you have is always gonna be true because ++col is always gonna be 1. I you want to check if it is 1 and then add one you should use col++ == 1.
I tried it out and the way I did it is open file

1
2
ifstream input;   
input.open("catalan.txt")


then use a do while loop to get the word and meaning. Using
1
2
3
4
5
6
7
do
{
input>> word[i]
getline(input,meaning[i])
++i
}
while (!input.eof());


this should get each word in word[i]
and each meaning into a variable meaning[i]

Last edited on
Topic archived. No new replies allowed.