Nowe posty

xx Dystrybucja pod HP Omen (6)
Wczoraj o 23:30:08
xx [Poradnik] Wyszukiwanie Sterowników (2)
Wczoraj o 21:08:23
lamp Problem z Linux Lite po instalacji (0)
Wczoraj o 19:50:30
xx Ile pingwinów? (1)
Wczoraj o 08:59:24
xx konfiguracja pale moon (0)
2024-03-24, 21:53:42
xx Plasma 6 w Neonie ssie trochę mniej ... (10)
2024-03-23, 02:38:11
xx problem z instalacja sterowników do karty sieciowej (3)
2024-03-18, 18:10:16
xx Plik abc.001 (1)
2024-03-17, 17:48:27
xx Zlecę dopracowanie programu w MatLab (0)
2024-03-13, 15:28:40
xx Linux Mint 21.3 XFCE brak dźwieku po paru minutach (karta muzyczna zintegrowana) (5)
2024-03-12, 23:07:01

Autor Wątek: zawartość pliku nie chce się skasować  (Przeczytany 2302 razy)

KelThuzad

  • Gość
zawartość pliku nie chce się skasować
« dnia: 2016-09-27, 21:51:58 »
Mam problem z nadpisanie pliku. Chce by pod koniec działania programu do pliku txt została przekazana nowa wartość i poprzednia dana została zmieniona. Niestety dana się nie zmienia a tylko jest dopisywana.

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
    {
        char choice = choice_of_payment.selec_cash_card_or_payment();
        if ( choice == '1' )
        {
            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 );

            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' ) );
    // to chyba powienie być file.clear(); ale nie mogr dodać.
    file.budget_save( budget );
    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 | std::ios::trunc );
}
//==========================================================================
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( double b )
{
    file << b << std::endl;
}
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 ( double b );
};

#endif // MY_FILE

KelThuzad

  • Gość
Odp: zawartość pliku nie chce się skasować
« Odpowiedź #1 dnia: 2016-09-29, 20:59:06 »
dodałem coś takiego
w my_file.h
void budget_save ( std::string name_file , double b );
    void close_file();
my_file.cpp
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();
}
Chodzi o to że po otwarciu pliki i przepisaniu do zmiennej budget wartości z pliku zamykam go i czyszczę file.clear() a pożnie otwieram ten sam plik do zapisu std::ios::out i std::ios::app. Ale dalej nie działa.