IFstream problem

Apr 18, 2015 at 1:25pm
Hello guys.I want to save numbers ,that i put in when cmd opens, in notepad but there are always false numbers.Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <winbgim.h>
#include <fstream>

using namespace std;

int main()
{
int po1[8];
int a;
int x;
ofstream izlaz("podaci.txt");

for (a=0;a<8;a=a+1)
{
    cin >> po1[x];
}
for (x=0;x<8;x=x+1)
{
    izlaz << po1[x];
    izlaz << " ";
}

system("PAUSE");
return 0;
}
Apr 18, 2015 at 1:37pm
The short answer is that you add izlaz.close(); at line 25.

The long answer is that when you write to a stream, the program doesn't immediately write to the file. This is for performance reasons: reading & writing to a disk is around a MILLION times slower than reading/writing to/from memory. Adding the call to close() will cause the program to write the data out to the operating system and from there notepad will see it.
Apr 18, 2015 at 3:27pm
I added izlaz.close(); at line 25 but it's the same.
Apr 21, 2015 at 2:37am
Ah! The problem is line 18. It should be cin >> pol[a]; ("a", not "x")
Topic archived. No new replies allowed.