reading into array/2d array from mixed text file..
I know to read a strings into array and tables.. what is they are mixed up?? strings are just names ( 3 characters) and there are bunch of table.. the max size was set to 60
ex. text file
JES
DAN
JEN
.
.
.
01010101
10010101
RAM
JET
01010010
10100101
.... and so on
should i use a while loop?
Re: reading into array/2d array from mixed text file..
Do you want the names to be read into one array and the binary numbers into another - or do you want them all read into just the one array? It would be better to use a vector rather than a fixed sized array. If you just want to read the data into a vector then something like this would do
Code:
open file and test for error
while (data extraction is good) {
add data to vector
}
Re: reading into array/2d array from mixed text file..
Pretend you're us, with no idea what you're trying to do. Read your post and see if it makes sense. Kudos to 2kaud for taking a shot at it, but I have no idea what you're asking.
Re: reading into array/2d array from mixed text file..
Quote:
Originally Posted by
2kaud
Do you want the names to be read into one array and the binary numbers into another - or do you want them all read into just the one array? It would be better to use a vector rather than a fixed sized array. If you just want to read the data into a vector then something like this would do
Code:
open file and test for error
while (data extraction is good) {
add data to vector
}
yeah there are 2 different arrays.. string names[60] and table[60][60].. but they are mixed up.. how do i make sure.. names get read into string and numbers into table??
Code:
for (int i = 0; i < size; i++)
{
myfile >> students[size];
myfile >> table[size][size];
}
this isnt working
Re: reading into array/2d array from mixed text file..
This will always extract the data into the array one element past its end!
Is there a pattern to how the names and numbers appear in the file? Or is the arrangemen of names/nunbers somewhat random? If there is a known pattern, then the extraction code can make use of this. If there's not then you will need to read each item from the file as a string and then determine whether it is a name or a number and assign to the appropriate array element.
Re: reading into array/2d array from mixed text file..
the names are only 3 letters, but the teacher said array should be able to hold up to 60 names.. she gave us a new file.. so now the file has 15 names and table is also 15 x 15 but the table should also be able to go 60 x 60.
file.txt
{15 names}
JAN
DAN
.
.
.
{ 15 X 15 } tabele
101010101010101
101010101010101
101010101010101
...
...
Re: reading into array/2d array from mixed text file..
and your c++ problem is? Design wise, it's a simple matter of reading first the names and then the digits and storing them in the array.
See http://forums.codeguru.com/showthrea...ork-assignment
Re: reading into array/2d array from mixed text file..
Quote:
Originally Posted by
2kaud
my hw is wayyy different.. im just trying to learn how to read more than one different data types from a file.. until now we only learned reading int..
Re: reading into array/2d array from mixed text file..
To read different of kinds of data types from a file, there are basically two ways. If it is a text file and you know the format of the data then you just extract the data using stream extraction to variables of the required type. eg
Code:
int i;
string s;
infile >> i >> s;
assuming the file contains
then i will contain 123 and s will contain abc.
If you don't know in advance the format of the data in the file, then you need to extract all data as a type string and then check the format of the data read (eg is the first char of the string a letter or a digit etc) to find out what type it should be and do an appropriate conversion.
Re: reading into array/2d array from mixed text file..
Quote:
Originally Posted by
2kaud
To read different of kinds of data types from a file, there are basically two ways. If it is a text file and you know the format of the data then you just extract the data using stream extraction to variables of the required type. eg
Code:
int i;
string s;
infile >> i >> s;
assuming the file contains
then i will contain 123 and s will contain abc.
If you don't know in advance the format of the data in the file, then you need to extract all data as a type string and then check the format of the data read (eg is the first char of the string a letter or a digit etc) to find out what type it should be and do an appropriate conversion.
Right now all the data is stored as string.. How do I extract? Because 1's and 0's are also stored as lines in string using get line... Whereas they should be stored in 2D array table. Thank you
Re: reading into array/2d array from mixed text file..
So if the first char of the string read is a '1' or a '0' then you have a binary number. Simply iterate through the string and place each char in the required position in the array. Each row will hold a number with the columns holding the individual elements. So the row number will increase for each string read and the col number will start at 0 and increase for each element of the string.