No data written to file? I/O question.

The file is open, the program has exclusive access to the file, no data is written at all. Even the TestLine fails to write.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
Save.open("SaveFile.mystery");
if (!Save.is_open())
{
cout<< "Not open.";
getch();
}
Ans = 0;
while(Ans != 27)
{
Save<< SAVE[Ans] << "\n";
Save<< "TestLine";
Ans = Ans +1;
}
Save.close();
What type is Save?
Save is an fstream. SAVE is a double float.
Are you certain the file is open? Line 2 will evaluate to false if SaveFile.mystery does not exist prior to you trying to create it with the open call. Flushing cout before calling getch might help you see that, although one wonders why you allow the program to continue past line 5 if the file is not opened.

The file is certainly open, the file certainly exists and the program lets me past line 5.
Flushing cout before calling getch might help you see that

What is "flushing"?
Oh, I forgot to mention that the if evaluation was added as a test to see whether the file is open or not, it will not be in the final code.
Save.open needs its flags when using fstream. Add std::ios::out as a parameter to open.
The file is certainly open, the file certainly exists and the program lets me past line 5.

Of course it lets you get past line 5. It would do so regardless of whether or not the file exists or is open.

Since you aren't supplying code which can be compiled, try the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <iostream>

int main()
{
    using std::cout;

    double SAVE [27] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
    
    std::fstream Save("SaveFile.mystery");

    if (!Save.is_open())
    {
        cout << "Not open.\n";
        return 0;
    }

    cout << "The file was successfully opened.\n";

    int Ans = 0;
    while (Ans != 5)
        Save << SAVE[Ans++] << "\n";
}


What is the result?
Adding std::ios::out as a parameter did nothing...

All code for program (windows)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <conio.h>
#include <string>
#include <sstream>


using namespace std;

int main()
{
int Clock = 0;
int ClockMax = 0;
int x = 0;
int y = 0;
int Action = 0;
int While = 0;
int key = 0;
int Ans = 0;
string line = "0";
double SAVE[27];
fstream Save("SaveFile.mystery");
while(getline(Save, line))
{
SAVE[x] = atof(line.c_str());
x = x +1;
}
Save.close();
if (SAVE[26] != 1)
{
x = 0;
while(x != 26)
{
SAVE[x] = 0;
x = x +1;
}
SAVE[0] = SAVE[4] = SAVE[6] = SAVE[8] = SAVE[15] = 100;
SAVE[26] = 1;
}
While = 0;
x = 1;
//load
L00:
Save.open("SaveFile.mystery", std::fstream::out);
if (!Save.is_open())
{
cout<< "Not open.";
getch();
}
Ans = 0;
while(Ans != 27)
{
Save<< SAVE[Ans] << "\n";
Save<< "TestLine";
Ans = Ans +1;
}
Save.close();
system("cls");
key = 0;
if (x == 1)
cout<< "\x10";
cout<< "Store\n";
if (x == 2)
cout<< "\x10";
cout<< "Common house\n";
key = getch();
if (key == 72)
x = x -1;
if (key == 80)
x = x +1;
if (x < 1)
x = 2;
if (x > 2)
x = 1;
if (key == 92)
{
system("cls");
getch();
}
if (key == 122)
goto L01;
goto L00;
L01:
system("cls");
}
Last edited on
The file was successfully opened.

Contents:

1
2
3
4
5
I just found the issue, I have to use both ifstream and ofstream variables and make sure not to open a file twice.
Topic archived. No new replies allowed.