Witam ma problem ze zmiana wartości w pliku. Chce by plik przechowywał zmienna budget i po każdym wykonaniu programu zmieniała tę wartość. Ale nie mogę tego osiągnąć. Na innym forum znalazłem pewien sposób. Najpierw trzeba plik odtworzyć do odczytu i jak już dana z pliku będzie w zmiennej budget i zamykam plik i go czyszczę. Następnie pod koniec programy otwieram plik do zapisu i tam chce nadpisać plik ale to rozwiązanie nie wiem czemu mi nie działa?
main.cpp:
#include "my_file.h"
#include "transactions.h"
#include "date.h"
#include "my_file_out.h"
#include <iostream>
#include <fstream> //DO KORZYSTANIA Z PLIKOW
#include <iomanip> //DO SETPRECISION
#include <stdexcept> //OBSLUGA BLEDOW
#include <string>
#include <sstream> //STRINGBUFFER
int main()
{
my_file file( "dane.txt" ) ;
My_file_out file_final ;
if ( file.is_file_opened() )
{
std::cerr << "Plik nie istnieje" << std::endl;
std::exit ( EXIT_FAILURE ); //KONCZY PROGRAM
// return 1;
}
if ( file.is_file_empty() )
{
std::cerr << "Plik jest pusty" << std::endl;
std::exit ( EXIT_FAILURE ); //KONCZY PROGRAM
// return 1;
}
double budget = {};
try
{
budget = std::stod( file.add_budget() );
}
catch ( const std::invalid_argument&)
{
std::cerr << "Dane w pliku nie są liczbami" << std::endl;
std::exit ( EXIT_FAILURE ); //KONCZY PROGRAM
}
std::cout << "budzet na starcie wynosi: " << std::setprecision(5) << budget << std::endl;
transactions choice_of_payment , cost;
date day_payment , month_payment , year_payment , buy ;
int day , month , year ;
double payment = {};
char answer = {};
std::stringstream date_final , string_budget , string_cost ;
std::string what_buy = {} , purchase = {} ;
do
{
file.close_file();
char choice = choice_of_payment.selec_cash_card_or_payment();
if ( choice == '1' )
{
purchase.clear();
what_buy = {};
string_cost.str(""); //Czyszczenie stringstream
date_final.str(""); //Czyszczenie stringstream
string_budget.str(""); //Czyszczenie stringstream
std::cout << "Wybrales\aa zaplate przez gotowke" << std::endl;
std::cout << "Podaj date w ktorym zaplaciles\as gotowka" << std::endl;
day = day_payment.day();
month = month_payment.month();
year = year_payment.year();
date_final << day << "-" << month << "-" << year;
std::cout << date_final.str() << std::endl;
what_buy = buy.what_buying();
std::cout << "Ile zaplaciles\as: ";
std::cin >> cost.cost_of_purchasing;
budget = cost.payment_cash( budget , cost.cost_of_purchasing );
string_budget << budget;
string_cost << cost.cost_of_purchasing;
std::cout << "Teraz budzet wynosi: " << budget << std::endl;
purchase = what_buy + " " + string_cost.str() + " " + date_final.str() + " " + string_budget.str();
std::cout << purchase << std::endl;
file_final.file_to_save( purchase );
// file.budget_save( budget );
do
{
std::cout << "Czy chce zakonczyc program, jezeli tak to wpisz Y - yes" << std::endl;
std::cout << "Jezeli nie chcesz zakonczyc to napisz N - no" << std::endl;
std::cin >> answer;
}
while ( !((answer == 'Y' || answer == 'y') || (answer == 'N' || answer == 'n') ));
}
else if ( choice == '2' )
{
std::cout << "Wybrales\as zaplate przez carte bankowa" << std::endl;
}
else if ( choice == '3' )
{
std::cout << "Wybrales\as wplate gotowki na konto" << std::endl;
payment = choice_of_payment.payment_cash_to_card();
budget += payment;
std::cout << "Wplaciles/as na konto: " << payment << " teraz na koncie masz: " << budget << std::endl;
do
{
std::cout << "Czy chce zakonczyc program, jezeli tak to wpisz Y - yes" << std::endl;
std::cout << "Jezeli nie chcesz zakonczyc to napisz N - no" << std::endl;
std::cin >> answer;
}
while ( !((answer == 'Y' || answer == 'y') || (answer == 'N' || answer == 'n') ));
}
else
{
std::cerr << "Wybrales\as zla opcje" << std::endl;
}
}
while ( !( answer == 'Y' || answer == 'y' ) );
file.budget_save( "dane.txt" , budget );
file.close_file();
return 0;
}
my_file.cpp
#include "my_file.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <stdexcept>
my_file::my_file(std::string name_file)
{
file.open( name_file , std::ios::in );
}
//==========================================================================
bool my_file::is_file_opened()
{
if ( !file.good() )
{
return true;
}
}
//==========================================================================
bool my_file::is_file_empty()
{
file.seekp( 0 , std::ios::end );
size_t size = file.tellg();
if ( size == 0 )
{
std::cout << "Plik jest pusty" << std::endl;
}
file.seekp(0 , std::ios::beg);
return ( size == 0 );
}
//==========================================================================
std::string my_file::add_budget()
{
std::string line;
std::getline( file , line );
return line;
}
//==========================================================================
void my_file::budget_save( std::string name_file , double b )
{
file.open( name_file , std::ios::out | std::ios::app );
file << b << std::endl;
}
//==========================================================================
void my_file::close_file()
{
file.close();
file.clear();
}
my_file.h
#ifndef MY_FILE
#define MY_FILE
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdexcept>
class my_file
{
private:
std::fstream file;
public:
my_file ( std::string name_file );
bool is_file_opened();
bool is_file_empty();
std::string add_budget();
void budget_save ( std::string name_file , double b );
void close_file();
};
#endif // MY_FILE
Jak coś to wiem że kod jest nie czytelny dopiero się uczę i najpierw chce napisać program by działał a później będę go modyfikował.