Why program doesn't read from text file?

Hello, I have a big problem. My program cant read anything from Text file. Here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

const char AB[] = "Klases.txt"; // duom
const char CD[] = "Mokytojai.txt"; // duom
const char EF[] = "Pamokos.txt"; // duom
const char FR[] = "Tvarkarastis.txt"; // rez
const char MAX = 100;
//------------------------------------------------------------------------------
class Pamoka
{
private: 
	 string klase;
	 string diena;
	 int nr;
	 string pamoka;
public:
	void Deti(string klase, string diena, int nr, string pamoka);
	 string ImtiKlase(){return klase;}
	 string ImtiDiena(){return diena;}
	 int ImtiNr(){return nr;}
	 string ImtiPamoka(){return pamoka;}
};
//-----------------------------------------------------------------------------
void Pamoka::Deti(string klase, string diena, int nr, string pamoka){
	 klase=klase;
	 diena=diena;
	 nr=nr;
     pamoka=pamoka;
}
//---------------------------------------------------------------------------
class MokPamokos{
private:
	string diena2;
	int kelPam;
	string uzimt;
public:
	void Deti2(string diena2, int kelPam, string uzimt);
	string ImtiDiena2(){return diena2;}
	int ImtiKelPam2(){return kelPam;}
	string ImtiUzimt2(){return uzimt;}
};
//---------------------------------------------------------------------
void MokPamokos::Deti2(string diena2, int kelPam, string uzimt){
	 diena2=diena2;
	 kelPam=kelPam;
	 uzimt=uzimt;
}
//-----------------------------------------------------------------------
class GeriausiosPamokos{
private:
	string diena3;
	int nr3;
public:
	void Deti3(string diena3, int nr3);
	string ImtiDiena3(){return diena3;}
	int ImtiNr3(){return nr3;}
};
void GeriausiosPamokos::Deti3(string diena3, int nr3){
	 diena3=diena3;
	 nr3=nr3;
}
//------------------------------------------------------------------------------
void Pamokos(const char AB[], Pamoka p[], int i, int & sum);
void SpausdintiPamokas(const char FR[], Pamoka p[], int sum);
void SkaitytiMokytojai(ifstream & fd2, MokPamokos m[], int i, int & sum2);
void SpausdintiSkaitytiMokytojai(const char FR[], MokPamokos m[], int sum2);
void SkaitytiGerPam(ifstream & fd3, GeriausiosPamokos g[], int i, int & sum3);
void SpausdintiGerPam(const char FR[], GeriausiosPamokos g[], int i, int sum3);

int main(){

	setlocale(LC_ALL, "Lithuanian");
	int i=0, n1=0;
	int sum=0, sum2=0, sum3=0;
    Pamoka p[MAX];
	MokPamokos m[MAX];
	GeriausiosPamokos g[MAX];

    ifstream fd1(AB);
	ifstream fd2(CD);
	ifstream fd3(EF);
	ofstream fr(FR); fr.close();
	
	Pamokos(AB, p, i, sum);
	SpausdintiPamokas(FR, p, sum);
	SkaitytiMokytojai(fd2, m, i, sum2);
	SpausdintiSkaitytiMokytojai(FR, m, sum2);
	SkaitytiGerPam(fd3, g, i ,sum);
	SpausdintiGerPam(FR, g, i, sum3);

	fd1.close();
	fd2.close();
	fd3.close();
	
	cin.ignore();
	return 0;
}

void Pamokos(const char AB[], Pamoka p[], int i, int & sum){
	string klase, diena, pamoka;
    int nr;
	ifstream fd1(AB);
		while(!fd1.eof()){
			fd1.getline();
			fd1 >> klase >> diena >> nr >> pamoka;
			p[i].Deti(klase, diena, nr, pamoka);
		i++;
		sum++;
cout << sum << endl;
	}
	fd1.close();

}
void SpausdintiPamokas(const char FR[], Pamoka p[], int sum){
	ofstream fr(FR, ios::app);
		for(int i=0; i<sum; i++){
			fr << p[i].ImtiKlase() << " " << p[i].ImtiDiena() << " " << p[i].ImtiNr() << " " << p[i].ImtiPamoka() << endl;
			//cout << p[i].ImtiKlase() << " " << p[i].ImtiDiena() << " " << p[i].ImtiNr() << " " << p[i].ImtiPamoka() << endl;
		}
	fr.close();
}
void SkaitytiMokytojai(ifstream & fd2, MokPamokos m[], int i, int & sum2){
	string diena2;
	int kelPam;
	string uzimt;
		while(!fd2.eof()){
			fd2 >> diena2 >> kelPam >> uzimt;
			m[i].Deti2(diena2, kelPam, uzimt);
			//cout << m[i].ImtiDiena2() << " " << m[i].ImtiKelPam2() << " " << m[i].ImtiUzimt2() << endl;
			i++;
			sum2++;
		}
		fd2.close();
}
void SpausdintiSkaitytiMokytojai(const char FR[], MokPamokos m[], int sum2){
	ofstream fr(FR, ios::app);
	for(int i=0; i<sum2; i++){
		fr << m[i].ImtiDiena2() << " " << m[i].ImtiKelPam2() << " " << m[i].ImtiUzimt2() << endl;
		//cout << m[i].ImtiDiena2() << " " << m[i].ImtiKelPam2() << " " << m[i].ImtiUzimt2() << endl;
	}
	fr.close();
}
void SkaitytiGerPam(ifstream & fd3, GeriausiosPamokos g[], int i, int & sum3){
	string diena3;
	int nr3;
	while(!fd3.eof()){
		fd3 >> diena3 >> nr3;
		g[i].Deti3(diena3, nr3);
		//cout << g[i].ImtiDiena3() << " " << g[i].ImtiNr3() << endl;
		i++;
		sum3++;
	}
	fd3.close();
}
void SpausdintiGerPam(const char FR[], GeriausiosPamokos g[], int i, int sum3){
	ofstream fr(FR, ios::app);
	for(int i=0; i<sum3; i++){
		fr << g[i].ImtiDiena3() << " " << g[i].ImtiNr3() << endl;
	}
	fr.close();
}
Sorry I'm from Lithuania, so sorry for poor English.
Last edited on
Are the files in the same directory as the executable?
What part of your code is not working? Show sample input. Where did you read the file in your program, and where did you print to the file?
Files are in the same directory as the executable. For examle in 108 line my code is reading a file until the end but i want that my code would read until the next line... So how it should be? Sorry for poor English :(
Any solutions? I don't understand why when I'm trying to cout for example
cout << p[i].ImtiKlase() << " " << p[i].ImtiDiena() << " " << p[i].ImtiNr() << " " << p[i].ImtiPamoka() << endl; at 123 line
i get a lot of numbers? What is wrong?
Last edited on
Topic archived. No new replies allowed.