#include <iostream>
#include <fstream> // Required, so we can use ofstream
#include <sstream> // Required, so we can use std::stringstream
using namespace std;
int main()
{
// For statement for looping 10 times
for(int i=1; i<=10; i++)
{
// Tell the user what file number we are working with
cout << "File number: " << i << endl;
// Create a ofstream, which can be used to write into files
ofstream file;
// Create a std::stringstream, to which we can input information
std::stringstream stream;
// Input the filenumber and the file type to the stream
// For example, if the value of i was 5, the content of stream would be "5.pbm"
stream << i << ".pbm";
// This looks ugly, the stream is converted into std::string, which is converted into char*, which is needed for this function,
// because it won't accept std::stringstream or std::string types
file.open(stream.str().data());
// Warn the user if the file couldn't be opened
if (file.is_open() == false)
cout << "Couldn't open the file\n";
// Define the file is a PBM (portable bitmap) with a magic number
file << "P1\n";
// Define the width and height of the image ( width is 6 and height is 4 )
file << "6 4\n";
// Let's input the actual data ( \n is used to mark new lines )
file << "1 0 1 0 1 0\n";
file << "1 1 1 0 1 0\n";
file << "1 1 1 0 1 0\n";
file << "1 0 1 0 1 0";
// Save and close the file
file.close();
}
// Informs the program has executed successfully and will now close
return 0;
}
This program should create ten .pbm files.
I'll have to admit I'm not very familiar with std, so the file.open(stream.str().data()) portion could be probably done in a better way.