Jak zamienić wartość w pliku

0

witam ma problem z plikiem który słóży mi do wczytana zmiennej do programu i (chciałem to zrobić ale nie działa) zmiany tej zmiennej pod koniec programy.

plik to
my_file.h

#ifndef MY_FILE
#define MY_FILE
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdexcept>

class my_file
{
private:
    std::fstream file;
//    std::fstream file_final;
public:
    my_file ( std::string name_file );
    bool is_file_opened();
    bool is_file_empty();
    std::string add_budget();
    void budget_save ( double b );
    void close_file();
};

#endif // MY_FILE

 

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::out );
}
//=========================================================================
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;
}
//==========================================================================
void my_file::close_file()
{
    file.close();
}

w main wywołuje tak

 file.budget_save( budget );

Ale mam coś takiego ze w pierwsze linijce mam 800 a później np. 780 zamiast tylko w pliku mieć 780.

0

Jezu, czytaj swoje posty przed wrzuceniem ich w sieć - następny napisany takim koślawym polskim poleci prosto do kosza.Twój problem, jak mniemam, polega między innymi na tym, iż nigdzie nie usuwasz zawartości pliku, zatem automatycznie wszystko jest zapisywane na jego koniec.

0

Czy chodzi Tobie o ios::trunc?

1 użytkowników online, w tym zalogowanych: 0, gości: 1