Writing to a DATA FILE

Can someone tell me why my code is not writing to a data file! I get no errors and everything looks great!

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
#include "Account.h"
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function

using std::ofstream;


int main() {
	ofstream outdata;
	const int SIZE = 6;

	//Account array
	Account Account_array[SIZE] = { Account(832442, 560.10, .02),
									Account(832443, 1020.58, .04),
									Account(832444, 78.00, .02),
									Account(832447, 1200.12, .04),
									Account(832449, 489.33, .02),
									Account(833001, 105.74, .02)};
		
	// Loop to display Account details
		for (int x=0; x<6; x++) {
			Account_array[x].account_details();
		}

		outdata.open("AccountDetails.dat"); // Opens the file
		if ( !outdata ) {	//File could not be opened
			cout << "Error: file could not be opened" << endl;
			exit(1);
		

		for (int x=0; x<6; x++) {
			Account_array[x].account_details();
			outdata.close();
		}
		}
			return 0;
		}


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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

//Account class
class Account {
private:
	int number;
	double balance;
	double rate;

public:
	Account(); // Constructor
	Account (int, double, double);
	void setNumber(int);
    void setBalance(double);
    void setRate(double);

	int getNumber();
    double getBalance();
    double getRate();
	void account_details();
	string string_account_details();
};

Account::Account()
{
	number =0;
	balance = 0;
	rate = 0;
}

Account::Account(int number, double balance, double rate) {
                Account::number = number;
                Account::balance = balance;
				Account::rate= rate;
}
void Account::setNumber(int number) {
    Account::number = number;
}

void Account::setBalance(double balance) {
    Account::balance = balance;
}

void Account::setRate(double rate) {
    Account::rate = rate;
}

int Account::getNumber() {
    return number;
}
double Account::getBalance() {
    return balance;
}
double Account::getRate() {
    return rate;
}

void Account::account_details() {
	cout << "The current account number is " << number <<
		" with a balance of $" << balance << " and an interest rate of " << rate << ".\n\n";
}


closed account (S6k9GNh0)
What line did you write the data to file?
Between line 26-34 of the first code
I don't think you can include the definitions of functions inside a header file. Declarations yes, but definitions... I think it has to be in the C++ file. I'm not 100% sure, but... I think that may be the problem.

-Albatross
Last edited on
closed account (S6k9GNh0)
All he did was open the file. No function here is writing data to the file.

http://www.cplusplus.com/reference/iostream/fstream/

You can define functions inside of the header file, but they're all inline at that point. There's no immediate problem with it at this case. Although, for reference sake, if this were a larger project, he would need to look into it to optimize executable size and speed.
Last edited on
How do i get it to write to the data file?? Because I need it to read from it next!
@computerquip

I know that you can declare them in the header file, but I've never heard of or seen anyone define them without using #define . I'll have to look into that...

And you're perfectly right, when I look again, he didn't write anything before closing the file at line 34.

@tarhaeelfan08

Just look at the included reference, and I'm sure you'll be fine.

-Albatross
Last edited on
Is this better??
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
#include "Account.h"
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function

using std::ofstream;

int main() {


	ofstream outdata;
	const int SIZE = 6;


	//Account array
	Account Account_array[SIZE] = { Account(832442, 560.10, .02),
									Account(832443, 1020.58, .04),
									Account(832444, 78.00, .02),
									Account(832447, 1200.12, .04),
									Account(832449, 489.33, .02),
									Account(833001, 105.74, .02)};
		
	// Loop to display Account details
		for (int x=0; x<6; x++) {
			Account_array[x].account_details();
		


		ofstream myfile;
		myfile.open ("Account.data");
		myfile << Account_array[x].account_details();
		myfile.close();
		}

		}


			return 0;
		}

Try myfile.write? I don't know, I prefer stdio.h, and use that far more often.

-Albatross
How is this
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
#include "Account.h"
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function

using std::ofstream;

int main() {


	ofstream outdata;
	const int SIZE = 6;


	//Account array
	Account Account_array[SIZE] = { Account(832442, 560.10, .02),
									Account(832443, 1020.58, .04),
									Account(832444, 78.00, .02),
									Account(832447, 1200.12, .04),
									Account(832449, 489.33, .02),
									Account(833001, 105.74, .02)};
		
	// Loop to display Account details
		for (int x=0; x<6; x++) {
			Account_array[x].account_details();
		}


		ofstream file ("AccountDetail.data");
		if(!file)
        {
                cerr<<"ERROR"<<endl;
                exit(0);
        }
		Account_array[SIZE].account_OutPutdetails(file);
		file.close();

		cin.ignore();
		cin.get();


			return 0;
		}


My data file is showing as this when I open in a text pad.

-858993460
-9.25596e+061
5.43402e-304
Well, try messing around with your account values and see if your function keeps up.

-Albatross
closed account (S6k9GNh0)
Steps on writing to a file:

1) Open the file with correct mode:
http://cplusplus.com/reference/iostream/ofstream/ofstream/

2) The file now needs to be checked for validity. If it is, write to the file:
http://cplusplus.com/reference/iostream/ios/good/

3) We may now write to the file. There are several ways of doing this. write() and put() are valid ways of inserting unformatted data. Or we may use something the chute-like << operator to insert our data.
http://cplusplus.com/reference/iostream/ostream/operator%3C%3C/
http://cplusplus.com/reference/iostream/ostream/put/
http://cplusplus.com/reference/iostream/ostream/write/

4) Now that we've written to our file, please close the file to disassociate with the stream and to automatically flush everything into our file.

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
#include "Account.h"
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function

using std::ofstream;

int main() {


	ofstream outdata;
	const int SIZE = 6; //This is generally a bad idea in C++.


	//Even though this is hardcoded, to add function and flexibility, I would use a vector.
	Account Account_array[SIZE] = {    Account(832442, 560.10, .02),
							        Account(832443, 1020.58, .04),
								Account(832444, 78.00, .02),
								Account(832447, 1200.12, .04),
								Account(832449, 489.33, .02),
								Account(833001, 105.74, .02)  };
	
        //This is actually bad practice. Instead of hardcoding 6 here, use SIZE.
	//for (int x=0; x<6; x++) 
        for (int x = 0; x < SIZE; ++x)
        {
		Account_array[x].account_details();
                //I have no clue what this function does.
	}


	ofstream file ("AccountDetail.data");
	if(!file)
        {
                cerr<<"ERROR"<<endl;
                exit(0);
        }

        //Is this the function that outputs details using file?
        //If so, why don't you post this so I can give your more details on what your doing wrong.
	Account_array[SIZE].account_OutPutdetails(file);
	file.close();

	cin.ignore();
	cin.get();


	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <fstream>

int main()
{
     std::ofstream oFile("output");
     if ( oFile.good() )
     {
          oFile << "I am awesome. Can't touch this." << std::endl;
     }
     oFile.close();

     return 0;
}
Last edited on
Topic archived. No new replies allowed.