0

I`am currently studying Files and File streams in C++

My problem is:

I need to write number sequence (1-50 and 100-50) in file, and read data from it. Does anybody can explain me why, last 2 characters of my file read buffer is the same. And what should I do, to avoid it or what did I done wrong.

P.s. please be simply as possible :) I`am still learning!

code:

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    int up = 1;
    int down = 100;
    char buffer_1 [100];


        cout<<"1) Numbers that will be written in file #1: " <<endl;
    ofstream to_file_1("task1.txt"); //1.STEP
    while (up <=50)
        {
        cout<< up <<", " <<down <<", ";
        to_file_1<< up <<", " <<down <<", ";
        up++;
        down--;
    }
    to_file_1.close();

    cout<< endl <<endl <<"2)Data from file #1: " <<endl;
    ifstream out_of_file1 ("task1.txt"); //2.STEP
        while(!out_of_file1.eof())
        {
            out_of_file1 >> buffer_1;
            cout<<buffer_1 <<" ";
        }
    out_of_file1.close();

    return 0;
}
  • What 2 least characters are the same? It's working for me, everything is unique. – Stack Danny Oct 06 '16 at 19:43
  • No it is not! When reading characters (which is not convertet to number, yet) last 2 characters always is ... 51, 51 despite fact, that all previous is uniq, except last two –  Oct 06 '16 at 23:34

0 Answers0