Jump to content
  • Advertisement
Sign in to follow this  
dcuk

writing to file fprintf problems

This topic is 3668 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiya all, i can write in the file itself, appends just the way i want. It's just the output is ALL wrong. eg: 959723320 - Cust ID: 1752459607 Acc ID: 2002874980 Acc Type: (null) Amount: 0.000000 Prev Balance: 0.000000 New Balance: 0.000000 will post the entire class

#pragma once
#include <iostream>
#include <stdio.h>
#include <string>
//#include "account.h"
//using namespace std;
#include <time.h>



class Trans {
public:
	Trans();
	Trans(float amount) { Amount = amount; }
	//Trans(int time, float amount) { TransTime = time; TransTime = amount; } 
	~Trans() { fclose(pFile);}
	void Deposit(int AccountID, int aNumber, string type, float bal);
	void Withdraw(int AccountID, int aNumber, string type, float bal);
	void Transfer();
	void Authenticate();

	float getAmount() { return Amount; }
	//int getTransTime() { return TransTime; }
	int getID() { return transID; }

	void setTransactionID(int i) { transID = i; }
	void setAmount(float amount) { Amount = amount; }
	void setTime();

	bool Write(string str, int AccountID, int aNumber, string type, float bal);  //Account writing
	bool Write(string & str, int & AccountID, int & aNumber, string & type);            //Mortgage writing

	void showAll();
private:
	int transID;
	FILE * pFile;
	tm TransTime;
	float Amount;

};
Trans::Trans() {
	pFile = fopen ("transactions.txt","a+");
	Amount=0.0;

	time_t t = time(0);
	tm TransTime = *localtime(&t);
}

void Trans::setTime() {
	time_t t = time(0);
	tm TransTime = *localtime(&t);
}

void Trans::Deposit(int AccountID, int aNumber, string type, float bal)  { 
	Write("Deposit", AccountID, aNumber, type, bal);
}
void Trans::Withdraw(int AccountID, int aNumber, string type, float bal) {
	Write("Withdraw", AccountID, aNumber, type, bal);
}
void Trans::Transfer() { }
void Trans::Authenticate() { }
//WRITE METHOD FOR ACCOUNTS
bool Trans::Write(string str, int AccountID, int aNumber, string type, float bal) {
	int i = fprintf(pFile,"%i - %s Cust ID: %i Acc ID: %i Acc Type: %s Amount: %f Prev Balance: %f New Balance: %f\n",TransTime, str,AccountID,aNumber,type, Amount, bal, bal+Amount);
	cout << i;
	return true;
}

//WRITE METHOD FOR MORTGAGE SECTION
bool Trans::Write(string & str, int & AccountID, int & aNumber, string & type) {
	int i = fprintf(pFile,"%i - %s Cust ID: %i Acc ID: %i Acc Type: %s Amount: %f \n",&TransTime, &str,&AccountID,&aNumber,&type,&Amount);
	cout << i;
	return true;
}

void Trans::showAll() { 
	cout << "" << transID << "\tTime: " << TransTime.tm_mday << '/' << TransTime.tm_mon + 1 << '/' << TransTime.tm_year + 1900 << "\t\tAmount:: " << Amount << "\n";
}


Any help would be greatly appreciated, thanks

Share this post


Link to post
Share on other sites
Advertisement
You need to pass the printf parameters by value, not by pointer. As you have it, you are passing the adresses so printf is writing those addresses - not the values located there.

<edit> That is true for your second call but not your first.

Share this post


Link to post
Share on other sites
I have found out it doesnt like the string values. Outputting the values with cout << in the very same function. Works perfectly. It just doesnt like it writing it out to the file.

[Edited by - dcuk on November 27, 2008 12:12:58 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by dcuk
I have found out it doesnt like the string values. Outputting the values with cout << in the very same function. Works perfectly. It just doesnt like it writing it out to the file.

You can not print classes with fprintf. You must call c_str() method of the string first:
string something;
something = "abcdefg";
printf("string: %s", something.c_str());
I guess you are going to complain about format of timestamp as well, and for that case you had better convert it into text by using ctime()

Share this post


Link to post
Share on other sites
Or, y'know, use <fstream> and std::ofstream for the same syntax as std::cout, no issues with printing classes and no potential errors in your format string or omitting to close the stream.


#include <string>
#include <fstream>

int main()
{
int i=10;
std::string s="Hello";

std::ofstream os("file.txt");

if(os.fail()) handle_the_error();
else
{
os << "i=" << i << " and s=" << s << std::endl;
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!