I have been reading some of the posts on reading integers from files but I have not seen any on reading integers from formatted files. I have a file which is a 6 X 6 matrix consisting of integers separated by tabs. I want to read each number into an array so I can later do computations on each array element and then print them out in a new 6 X 6 matrix. I have been working on some code but I am not sure if I am going in the right direction.
Here is how it is suppose to work.
1. Ask user of the name of the file to open (done)
2. Read the first line of the matrix of 6 integers separated by tabs into an array.
3. Compute the new value of each element in the array using the metrics.
4. Send the new values to another function which will write the new values to a file.
5. Repeat steps 2-5 until reaching the end-of-file.
Here is the code I am reusing from an earlier project that worked with strings. I might be way off on this one so please bear with me.
//metric values
const int interface_speed = 25; //speed of the pc network cards
const int hop_value = 5; //number of connections per node
void create_metrics(ifstream &fin, int number[])
{
//write first row of numbers into the array
while (fin >> number[array_size])
{
//create new number
for(int i =0; i<array_size; i++)
{
number[i] = (number/interface_speed)/hop_value;
}
//get all the integers from the file...
while(fin.get(ch))
{
if(ch != ' ' && ch != '\n')//..until the next whitespace character or eof is found
{
number += ch; //add the value to number
}
else //if a whitespace is found do the following
{
//calls the function create_matrix
create_matrix(number);
//resets number to 0
number = 0;
//gets out of the loop
break;
}
}
}
}
//writes the output to the output file as a matrix with the metrics added
void create_matrix(int &number)
{
//still working on this one
}