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.
#include <iostream>
#include <fstream>
#include <string>
constint WIDTH = 3;
constint HEIGHT = 2;
// https://en.cppreference.com/w/cpp/language/type_aliasusing array_type = unsignedchar[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#forfor( constauto& row : array ) { // for each row in the file
// write each value in the row (as integer)
for( unsignedint 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
}
elsereturnfalse ; // failed to open file for output
}