M
M
marlaaa2022-04-21 05:32:48
C++ / C#
marlaaa, 2022-04-21 05:32:48

Why is the correct result displayed in the console, but not in the file?

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int filess() {
    int a[25]={0};
    ofstream file("proertyy.txt");
    for (int i = 0; i < 25; i++)
    {

            a[i] = rand() % 10 + 1;
            file << a[i];
        
    }
    return 0;
}
int main()
{
    filess();
    setlocale(LC_ALL, "Russian");
    
    ifstream readfile("proertyy.txt", ios::in | ios::binary);

    const int strings = 25;
    char mass[strings]={0};
    if (!readfile.is_open()) {
        cout << "Файл не открыт"<<endl;
    }
    else {
        cout << "Файл открыт"<<endl;
    }
    for (int i = 0; i < strings; i++)
    {
        readfile >> mass[i];

        cout <<mass[i] << endl;
    }

    int buff = 0; 
    int i, j; 
    for (i = 1; i < strings; i++)
    {
        buff = mass[i]; 
        for (j = i - 1; j >= 0 && mass[j] > buff; j--)
            mass[j + 1] = mass[j];

        mass[j + 1] = buff; 
    }

    ofstream file("proertyy.txt");
    for (int i = 0; i < strings; i++) {

        cout << mass[i];


        int a = mass[i];
        file << a;
    }
    readfile.close(); 
 
    return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
12rbah, 2022-04-21
@marlaaa

Do this and it will work

for (int i = 0; i < strings; i++) {
        cout << mass[i];
        file << mass[i];
    }

V
Vladimir Korotenko, 2022-04-21
@firedragon

What encoding? dos windows koi8r utf8

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question