|
Task of Program: I want to create this program that runs an input file (/forum/test.txt) that contains a matrix with dimensions (11 x11) meaning that there are 11 rows x 11 columns in the file called "/forum/test.txt". I want to read the exact dimension into an outfile (ofstream). However, After program is run, the final output is a huge line with all the data combined in the first line (0 0 0 0 0 0 0 0 16 0 0 0 0 0 0 0 0 0 0 0 16 16 16 16 16 16 0 0 0 0 0 0 0....). I am supposed to get the same table that I have in the input file (11 rows x 11 columns), but not a line. I have the right program because it runs perfectly in the output screen, but not when I want to create an outfile ("/forum/outdata.txt")What can I Do??
Example of the inputfile: ("/forum/test.txt") that I want to run into the new file:
0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0
0 0 1 1 1 1 1 0 0
0 1 1 1 1 1 1 1 0
0 0 1 1 1 1 1 0 0
0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0
Example of the outputfile ("/forum/outdata.txt") after program runs : 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1 1 .......0 0 0 0 0
/ Headers
#include <iostream>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <cstddef>
#include <cstdlib>
using namespace std;
// Define variables
typedef float value_type;
int main ()
{
ifstream infile;
infile.open("/forum/test.txt");
ofstream outfile;
outfile.open("/forum/outdata.txt");
value_type data[11*11];
copy(istream_iterator<value_type>(infile),istream_iterator<value_type>(),data);
// Initialize Program
int n=0,m=0;
for (n=0; n < 11; n++)
{
for (m=0; m < 11; m++)
{
outfile << data[(n*11+m)]<< endl;
}
std::clog << std::endl;
}
infile.close();
outfile.close();
return 0;
}
|
|
|
|
|
|
|
// |