I have this code that supposed to display the values from an array to a text file

#include <iostream>
    #include <cstdlib> 
    #include <fstream>
    #include <vector>
    using namespace std;

    int array[7][4];

    fstream file2;

    template< typename T > void display( const vector< vector<T> >& matrix )
    {
        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";
        }    
        file2 << "\n";

        file2.close();
    }

    template< typename T >
    vector< vector<T> > get( const vector< vector<T> >& m, size_t n,
                                   size_t i, size_t j )
    {
        const size_t N = m.size() ;
        vector< vector<T> > slice(n) ;

        for( size_t k = 0 ; k < n ; ++k )
            for( size_t l = 0 ; l < n ; ++l )
                slice[k].push_back( m.at(k+i).at(l+j) ) ;

        return slice ;
    }

    template< typename T > void print( const vector< vector<T> >& m )
    {
        file2.open( "file.txt", fstream::out);

        for( size_t i = 0 ; i < m.size() ; ++i )
        {
            for( size_t j = 0 ; j < m[i].size() ; ++j )
            {
                file2 << m[i][j] << " ";
            }              
            file2 << "\n";
        }
        file2 << "--------------------\n" ;
    }

    int main()
    { 
       //initialise array to random number not really shown here
       //to make the code shorter 
       int array1[16][16];

       vector< vector<int> > matrix(16);

       for(int b = 0; b < 16; ++b)
            matrix[b].assign( array1[b], array1[b] + 16);

       vector< vector<int> > b;

       for(int i = 0; i < 4; ++i)
       {
          b = get(matrix, 8, 8 * ( i/2 != 0 ), 8 * (i % 2));
          print(b);
          display(b);
       }

       return 0;

    }

Given a 16x16 array it should be divided into 4 blocks of 8x8 and display the result into a text file. No problem on that part. The problem is when I display the values of the array into the file it only displays the last block not all four blocks.

I tried replacing file2 with cout and everything works fine. I'm able to get the correct output.

I also tried file2.open( "file.txt", fstream::out | ios::app); also works but everytime I run the program it appends the data already in the file which makes my values redundant.

So my questions are:
How can I display all the values into the file? so that I can get the same output as when I use cout

How can I make sure that everytime I run my program the data in the files get overwritten?Nothing is appended whatsoever.
For example if the file contains the following:
1 2 3 4 5 6 7 8

when I run the program several times again it should contain the same thing.

Any tips on how I can solve this. Thanks!

move lines 9 and 43 down into main() then pass the file2 to print() as a parameter. Open the file only once, just before the loop that starts on line 69. Also, fstream objects must be passed to other functions by reference.

I think all of your problems stem from re-opening the file in your functions. Opening a file stream in write mode will truncate the file to zero length, so every time you call print(), the data in the file gets wiped out.

One way to fix it without significant changes is open the file in main() just before your second loop, remove the call to open() in print(), and finally remove the call to close() in display():

#include <iostream>
#include <cstdlib> 
#include <fstream>
#include <vector>
using namespace std;

int array[7][4];

fstream file2;

template< typename T > void display( const vector< vector<T> >& matrix )
{
    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";
    }    
    file2 << "\n";
}

template< typename T >
vector< vector<T> > get( const vector< vector<T> >& m, size_t n, size_t i, size_t j )
{
    const size_t N = m.size() ;
    vector< vector<T> > slice(n) ;

    for( size_t k = 0 ; k < n ; ++k )
        for( size_t l = 0 ; l < n ; ++l )
            slice[k].push_back( m.at(k+i).at(l+j) ) ;

    return slice ;
}

template< typename T > void print( const vector< vector<T> >& m )
{
    for( size_t i = 0 ; i < m.size() ; ++i )
    {
        for( size_t j = 0 ; j < m[i].size() ; ++j )
        {
            file2 << m[i][j] << " ";
        }              
        file2 << "\n";
    }
    file2 << "--------------------\n" ;
}

int main()
{ 
    //initialise array to random number not really shown here
    //to make the code shorter 
    int array1[16][16];

    vector< vector<int> > matrix(16);

    for(int b = 0; b < 16; ++b)
        matrix[b].assign( array1[b], array1[b] + 16);

    vector< vector<int> > b;

    file2.open( "test.txt", fstream::out);

    for(int i = 0; i < 4; ++i)
    {
        b = get(matrix, 8, 8 * ( i/2 != 0 ), 8 * (i % 2));
        print(b);
        display(b);
    }

    return 0;
}

THANKS A LOT got it to work now :) GOD BLESS!

ok so another problem I added some functions

#include <iostream>
#include <cstdlib> 
#include <fstream>
#include <vector>
using namespace std;

int array[7][4];
fstream file2;
fstream file1;

// checks if there are duplicate numbers and calls itself again if there are
void check(int n1, int n2, int a[][4])
{
    int b, c;

    if((n1 == 4) && (n2 == 3))
    {
        b = rand() % 7;
        c = rand() % 4;

        check(b, c, a);
    }
    else if((n1 == 5) && (n2 == 2) || (n1 == 5) && (n2 == 3)) 
    {
        b = rand() % 7;
        c = rand() % 4;

        check(b, c, a);
    }
    else if((n1 == 6) && (n2 == 1) || (n1 == 6) && (n2 == 2) ||
            (n1 == 6) && (n2 == 3))
    {
        b = rand() % 7;
        c = rand() % 4;

        check(b, c, a);     
    }
    else
    {
        file1 << n1 << " " << n2 << " ";
        file1 << a[n1][n2] << "\n";
    }
}

void random(int a[][4])
{
    srand(time(NULL));

    int first[4];
    int second[4];

    int c, b;

    //fstream file1;
    file1.open( "random.txt", fstream::out);

    file1 << "4 Random Numbers" << "\n";

    for( int i = 0; i < 4; i++)
    {
        first[i] = rand() % 7;
        second[i] = rand() % 4;

      if((first[i] == 4) && (second[i] == 3))
        {
            b = rand() % 7;
            c = rand() % 4;

            check(b, c, a);
        }
        else if((first[i] == 5) && (second[i] == 2) || (first[i] == 5) && (second[i] == 3) )
        {
            b = rand() % 7;
            c = rand() % 4;

            check(b, c, a);
        }
        else if((first[i] == 6) && (second[i] == 1) || (first[i] == 6) && (second[i]  == 2) || (first[i] == 6) && (second[i] == 3))
        {
            b = rand() % 7;
            c = rand() % 4;

            check(b, c, a);
        }
        else
        {
            file1 << first[i] << " " << second[i] << " ";  
            file1<< a[first[i]][second[i]] << "\n";
        }          
    }
    file1<< "\n";
}


template< typename T > void display( const vector< vector<T> >& matrix )
{
    // same as before
    random(array);
}
template< typename T >
vector< vector<T> > get( const vector< vector<T> >& m, size_t n, size_t i, size_t j )
{
    same as before
}
template< typename T > void print( const vector< vector<T> >& m )
{
   // same as before
}
int main()
{ 
    //lines here same thing as previous post    

    file2.open( "test.txt", fstream::out);
    for(int i = 0; i < 4; ++i)
    {
        b = get(matrix, 8, 8 * ( i/2 != 0 ), 8 * (i % 2));
        print(b);
        display(b);
    }
    return 0;
}

Now file1 is not updating.
The function display() calls the random() function with array as parameter. Now from the array it should generate 4 random numbers from the array. Since there are four 8x8 blocks. The file should contain 4 sets of random numbers.

I'm basically having the same problem as before.

here are the things that I tried doing by passing file1 as parameters to check check().

void random(int a[][4])
{
  // same lines here

  //declared file1 inside random
   fstream file1;
   file1.open( "random.txt", fstream::out);

   for( int i = 0; i < 4; i++)
   {
        first[i] = rand() % 7;
        second[i] = rand() % 4;

      if((first[i] == 4) && (second[i] == 3))
        {
            b = rand() % 7;
            c = rand() % 4;

            checke(b, c, a, file1);
        }
        // etc
   }
}



void check(int n1, int n2, int a[][4], fstream& file1)
{
    //statements same as before
}

ok already got it.haha

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.