Jul 25, 2012 at 8:19am UTC
I have this code that will display the results in a text file.
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
fstream file2; //declared globally
//print array
template < typename T > void print( vector< vector<T> >& a )
{
for ( size_t i = 0 ; i < a.size() ; ++i )
{
for ( size_t j = 0 ; j < a[i].size() ; ++j )
{
file2 << a[i][j] << " " ;
}
file2 << "\n" ;
}
}
template < typename T > void function2( const vector< vector<T> >& m )
{
// in this function I'm simply getting some parts from the array
// and then display the result to text file
int array[7][4];
file2 << array[i][j] << " " ;
file2 << "\n" ;
file2.close();
}
void function1()
{
file2.open( "sample.txt" , fstream::out);
// do something
vector< vector<int > > matrix(w); // w size of matrix
vector< vector<int > > a;
for (int n = 0; n < num; ++n)
{
for (int l = 0; l < num; ++l)
{
a = get(matrix, size, size * n, size * l);
file2 << "(" << size * n << ", " << size * l << ") " << "\n" ;
print(a);
function2(a);
}
}
}
The problem is it does not display everything in the text file. It only displays the first 8x8 array.
The text file should contain the following
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
// first block
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x
x x x x
x x x x
x x x x
x x x
x x
x
// second block
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x
x x x x
x x x x
x x x x
x x x
x x
x
...
Any tips on how to solve this?
Last edited on Jul 25, 2012 at 8:28am UTC
Jul 25, 2012 at 10:33am UTC
i'm sorry maybe it's me, but, i and j in function2 doesn't exist...
file2 << array[i][j] << " " ;
Jul 25, 2012 at 12:00pm UTC
I just didn't complete the statements in function2(). i and j are basically part of a double for loop which stores some elements of m in array[][].
I basically want to display all the elements in a text file using the format above.
Jul 25, 2012 at 12:50pm UTC
ah oh, are you sure that the line
file2.close();
is outside that loop?
Jul 25, 2012 at 1:10pm UTC
file2.close()
is inside function2()
Jul 25, 2012 at 1:36pm UTC
yes i can see that, what i meant was be sure that
file2.close();
is outside the double loop wich stores the elements in m
Jul 26, 2012 at 7:16am UTC
In function2 you write in to the file this array
int array[7][4];
are you sure that you initialise it with the m array, which is the input of the function?
Jul 26, 2012 at 8:15am UTC
yes
1 2 3 4 5 6 7 8 9 10 11
for (int i = 0; i < 7; ++i)
{
for (int j = 0; j < 4; ++j)
{
array[i][j] = matrix[i][j];
file2 << array[i][j] << " " ;
}
file2 << "\n" ;
}
Since I'm able to display everything using cout I don't know why file1 won't work. It only displays the first block and nothing else.
Last edited on Jul 26, 2012 at 8:20am UTC
Jul 26, 2012 at 8:37am UTC
Hi,
Maybe because you close your file in function2 and don't re-open it afteward ? The first iteration in the function1's loop close the file definitely.
Last edited on Jul 26, 2012 at 8:39am UTC
Jul 26, 2012 at 8:47am UTC
I tried not closing the file and it gives me the same thing. Where do you suggest I put the file2.close()?
Jul 26, 2012 at 9:10am UTC
Normally the file close automatically when the file I/O variable goes out of scope.. Here your file is globally declared, so you have to close it, if you put the file2.close() at the end of function1 that should be okay. You can also test if the file is still open with is_open() to debug more easily.
Last edited on Jul 26, 2012 at 9:15am UTC