eof() nie wczytuje całego pliku

0

Witam mam problem z wczytaniem calego pliku. A chodzi mi o taką sytuację kiedy plik wyglada tak:
pusta linia
pusta linia
pusta linia
800
itd.

W takim przypadku wczytanie danych nie chce mi działać ale kiedy jest tak:
800
pusta linia
pusta linia
pusta linia
itd.
Wtedy dana jest poprawnie wczytana. Nie wiem co się dzieje ale mam też dziwny problem na moim środowisku programistycznym (QT creator 3.5.1) kiedy wpisuje file.eof() to program podpowiada mi file ale już nie mogę uzyskać podpowiedzi na eof() wydaje mi się że pominąłem jakąś bibliotekę?

to jest tylko plik 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;
        while ( !file.eof() )
        {
            std::getline( file , line );
        }
    return line;
}

to jest plik 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();
};

#endif // MY_FILE

A to jest plik main.cpp

 #include "my_file.h"
#include "transactions.h"
#include <iostream>
#include <fstream>                                                      //DO KORZYSTANIA Z PLIKOW
#include <iomanip>                                                      //DO SETPRECISION
#include <stdexcept>                                                    //OBSLUGA BLEDOW
#include <string>
int main()
{
    my_file file( "dane.txt" );
    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;
    }
    //    std::string mm = file.add_budget() ;
    //    std::cout << "budrzet ma : " << mm << std::endl;
    double budget = {};
    //    std::cout << "budzet wynosi: " << budget << std::endl;
    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;
    //    budget = budget - 33.22;
    //    std::cout << "budget" << budget << std::endl;
    transactions choice_of_payment;
    int choice = {};
    choice = choice_of_payment.selec_cash_card_or_payment();
//    do
    if ( choice == 1 )
    {
        std::cout << "Wybrales\aa zaplate przez gotowke" << std::endl;
    }
    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;
    }
    else
    {
        std::string conver_int_to_string = std::to_string( choice );
        char const *conver_string_to_char = conver_int_to_string.c_str();
        std::cout << "co mam po zamianie z int na char " << conver_string_to_char << std::endl;
        if ( choice == 'a' )
        {
            std::cout << "mma to " << std::endl;
        }
        std::cerr << "Wybrales\as zla opcje" << std::endl;
    }
//    while ();

    return 0;
}

1

w std::string my_file::add_budget() wczytujesz wszystko do końca pliku i zwracasz ostatnią linię... Tak to wygląda, a spodziewam się, że chciałbyś by działało to inaczej.

0

ale dlaczego wczytuje tylko liczę jak jest w pierwszej linijce a jak jest w innej to nie?

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