Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

#include <fstream>
#include <string>
#include <iostream>
#include "Gyvūnas.h"
#include "Maistas.h"
using namespace std;

//-------------------------------------------------------
const char CFm[] = "Maistas.txt";
const char CFg[] = "Gyvūnai.txt";
const int CMax   = 100;
//-------------------------------------------------------

//--------------------------------------------------------
void Skaityti  (const char CFm[], Maistas M[], int & n);
void Skaityti2 (Gyvūnas G[], int & kg);
//----------------------------------------------------------
int main(){
    setlocale (LC_ALL , "Lithuanian");
    Maistas M[CMax];
    Gyvūnas G[CMax];
    int n;
    int kg;
    Skaityti  (CFm, M, n);
    Skaityti2 (G, kg);
    cout << M[1].ImtiMetus() << " " << n << endl;
    system ("PAUSE");
    return 0;
}
//----------------------------------------------------------
void Skaityti (const char CFm[], Maistas M[], int & n)
{
    string produktas;
    double kiekis;
    int metai;
    int m?nuo;
    int diena;
    ifstream fd(CFm);
    fd >> n;
    for (int i = 0 ; i<=n ; i++){ 
        fd >> produktas >> kiekis >> metai >> m?nuo >> diena;
        M[i].D?ti(produktas, kiekis, metai, m?nuo, diena);
    }
    fd.clear ();
    fd.close();
}
void Skaityti2 (Gyvūnas G[], int & kg)
{
    int narvas;
    string pavadinimas;
    int skai?ius;
    int produktas;
    int kiekis;
    int n;
    ifstream fd(CFg);
    fd >> n;
    for (int i = 0 ; i<=kg ; i++){
        fd >> narvas >> pavadinimas >> skai?ius >> produktas >> kiekis;
        G[i].D?ti(narvas, pavadinimas, skai?ius, produktas, kiekis);
    }
    kg = n;
    fd.close();
}

When I set breakpoints it shows that in this part ifstream cannot read variable n from file:

 ifstream fd(CFm);
    fd >> n;
    for (int i = 0 ; i<=n ; i++){ 
        fd >> produktas >> kiekis >> metai >> m?nuo >> diena;
        M[i].D?ti(produktas, kiekis, metai, m?nuo, diena);
    }
    fd.clear ();
    fd.close();

Errors are:

std::basic_ios

Filebuffer {_Set_eback=0xcccccccc _Set_egptr=0xcccccccc ...} std::basic_filebuf >

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
127 views
Welcome To Ask or Share your Answers For Others

1 Answer

What you have are not errors, just values of the pointers. It seems that you can't open CFm file. Please confirm if you really have Maistas.txt in the working directory (for test, you could just move it to C:Maistas.txt and then pass "C:\Maistas.txt" as absolute file path.

Also, there's a way to check if the ifstream is properly opened - you can do that by checking failbit:

ifstream fd(CFm);
if(!fd.good())
{
   cerr << "Could not open the file!" << endl;
   return;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...