Writing an array to a text file

Does anyone know how to output a 2d unsigned char array to a text file?

I have been trying things for hours now. The code compiles without any errors but the text file remains empty. Any help solving this issue would be appreciated.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int WIDTH = 3;
const int HEIGHT = 2;
const string FILE_NAME = "array.txt";

bool writeImage(const unsigned char ARRAY[][WIDTH], int height, const string fileName );


int main() {
    
    unsigned char array[HEIGHT][WIDTH] = {{1,2,3},{2,3,4}};
    writeImage(array, HEIGHT, FILE_NAME);
    
}

bool writeImage(const unsigned char ARRAY[][WIDTH], int height, const string fileName ){
    ofstream outputFile;
    outputFile.open(fileName);
    
    if (! outputFile) { 
        cout << "File " << fileName << " could not be opened.\n"; 
        cout << "program terminated.\n"; 
        return false; }
    

    for(int arrayRow = 0; arrayRow < height; arrayRow++){
        for(int arrayColumn = 0; arrayColumn < WIDTH; arrayColumn++){
            outputFile << ARRAY[arrayRow][arrayColumn] << " ";
        }
        outputFile << endl;
    }

    outputFile.close();
        
    return true;
    
}
Last edited on
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
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <fstream>
#include <string>

const int WIDTH = 3;
const int HEIGHT = 2;

// https://en.cppreference.com/w/cpp/language/type_alias
using array_type = unsigned char[HEIGHT][WIDTH] ;

bool writeImage( const array_type& array, const std::string& file_name );

int main() {

    const std::string file_name = "array.txt";

    const array_type array = { {1,2,3}, {2,3,4} };

    if( writeImage( array, file_name ) ) {

       #ifndef NDEBUG // if the program is being debugged

            // print a success message and dump the contents of the file on stdout
            std::cout << "write successful.\ncontents of file '" << file_name
                      << "' :\n-------------\n" ;

            // dump the contents of the file (dump the entire stream buffer)
            // https://en.cppreference.com/w/cpp/io/basic_ios/rdbuf
            std::cout << std::ifstream(file_name).rdbuf() << "-------------\n" ;

       #endif
    }

    else {

        std::cerr << "write failed\n" ;
        return 1 ; // return non-zero from main to indicate exit with failure
    }
}

bool writeImage( const array_type& array, const std::string& file_name ) {

    // if the file was opened successfully for output
    if( std::ofstream file{file_name} ) {

        // http://www.stroustrup.com/C++11FAQ.html#for
        for( const auto& row : array ) { // for each row in the file

            // write each value in the row (as integer)
            for( unsigned int value : row ) file << value << ' ' ;

            file << '\n' ; // and put a new line at the end of the row
        }

        return file.good() ; // write was successful if the file is still in a good state
    }

    else return false ; // failed to open file for output
}

http://coliru.stacked-crooked.com/a/2be250fa33625663
outputFile << (int) ARRAY[arrayRow][arrayColumn] << " ";
Topic archived. No new replies allowed.