I'm trying to read a file with 3 rows and 30 columns of chars into a two-dimensional array. I'm not really sure how that would work... could someone give me an idea of how to do it, or an example? Thanks.
This is something I wrote up really quick. Here is the text file I wrote up really quick to test run it:
1 2 3
a b c d e f g h i j k l m n o p q r s t u v w x y z aa bb cc dd
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
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
string str[30][3];
ifstream myfile("testfile.txt");
int a = 0;
int b = 0;
if(!myfile) //Always test the file open.
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}
while(!myfile.eof())
{
getline(myfile,str[a][b],' ');
if(a ==29)
{
a=0;
++b;
getline(myfile,str[a][b],' ');
}
a++;
}
system("pause");
}
Please note that I'm assuming that there are spaces in between each column, no spaces within a column, and one at the end of the last column. Things may need to be re-organized a bit if that is not the case. If you have any questions, just post back.