Beginner

Will this prgogram work?

#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

const char CDfv[] = "duomenys.txt"
const char CRfv[] = "rezultatas.txt"

int main()
{ int a, b, c, d, e, f;
ofstream fr(CRfv);
ifstream fd(CDfv);
fr.close();
fd >> a >> b >> c >> d;
e = a*b;
f = c*d;
fr << e << f;
fd.close();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <fstream> 
#include <iostream>
#include <iomanip> 

using namespace std;

const char CDfv[] = "duomenys.txt" 
const char CRfv[] = "rezultatas.txt"

int main() 
{
 int a, b, c, d, e, f; 
ofstream fr(CRfv); 
ifstream fd(CDfv); 
fr.close(); 
fd >> a >> b >> c >> d; 
e = a*b; 
f = c*d; 
fr << e << f; 
fd.close(); 
}
Last edited on
No, you've closed your stream before it's open (possible exception error) and then written/read to it when it's closed.

You need something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
fr.open();
fd.open();

if(fd.good()) // stream is open and not end of file
{
    // read code here
}

if(fr.is_open()) // stream is open for writing
{
    // write code here
}

fr.close();
fd.close();
Topic archived. No new replies allowed.