для windows:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
ofstream out;
out.open("C:\\123.txt", ios::out);
if (!out)
throw("file not open");
const int ARRAYSIZE = 15;
int arr[ARRAYSIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
string s;
char buf[5];
for (int i = 0; i < ARRAYSIZE; i++){
if (((i + 1) % 5) == 0){
itoa(arr[i], buf, 10); // переводим число в строку для msvs
s += buf;
s += "\n";
out << s.c_str();
s.clear();
} else {
itoa(arr[i], buf, 10); // переводим число в строку для msvs
s += buf;
s += " ";
}
}
if ((ARRAYSIZE % 5) != 0) // добавлено для размера массива не кратному пяти элементов
out << s.c_str();
out.close();
}
123.txt
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
______исправлено по совету @avp
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
ofstream out;
out.open("C:\\123.txt", ios::out);
if (!out)
throw("file not open");
const int ARRAYSIZE = 14;
int arr[ARRAYSIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
string s;
char buf[5];
for (int i = 0; i < ARRAYSIZE; i++){
itoa(arr[i], buf, 10);
out << buf << " ";
if (((i + 1) % 4) == 0){
out << "\n";
}
}
out.close();
}