I\O read txt file

hello , sorry for my english...In my project i have a problem ..

i don't know what the problem... the exe don't read txt file...

bool leggi_da_file(char* name,ifstream& f2){
int i=0;
string canc,nome,cognome,casa_edit;

do{
getline(f2,nome);
if(f2.eof()) return false;
if(nome!="#"){
getline(f2,cognome);
autori[i].nome=nome;
autori[i].cognome=cognome;
i++;
}if(i==5){ getline(f2,canc); break;}


}while(nome!="#");

autori[i].nome="";
contautori=i;
getline(f2,titolo);
getline(f2,casa_edit);
if(casa_edit=="*") casaed="";
casaed=casa_edit;
getline(f2,anno,'\n');
if(anno=="0") anno="";

return true;}
thanks ... sorry if i don't respect the forum lex
Last edited on
void leggilib(ifstream& f1){

int n_pag;

if(leggi_da_file("libri.txt",f1)==false) return;
string num;
getline(f1,num,'\n');
n_pag=atoi(num.c_str());
return;}
If you post your entire code with some examples of the input file I'm sure I could help.

Also please edit your post and format using "Source Code" tags.

Until then I recommend reading http://www.cplusplus.com/doc/tutorial/files/.

There seems to be some problems with your code, or I don't understand because of the missing parts.

char* name is not being used anywhere, or it's misspelled.

Seems to me your making it harder than it would need to be.

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
tanks before all...this is class and derivated class who using the function...



#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

struct autore{
       string nome;
       string cognome;
       };


       class pubblicazione{
            protected:

            autore autori[5];
            string titolo;
            string casaed;
            string anno;
            bool prestitoatt;
            int contautori;
            public:

            pubblicazione(){

            for(int i =0;i<5;i++){
            autori[i].nome="";
            autori[i].cognome="";
            }
            titolo="";
            casaed="";
            anno="";
            prestitoatt=false;
            }

            void aggiungi_infop(){

                 string j="";
                 int i=0;
                 bool ce=true;
                 do {
                 if(j=="n") break;
                 cout<<" -inserisci il nome del Cantante-Regista-Autore :\t";
                 getline(cin,autori[i].nome,'\n');
                 do{
                     if(autori[i].nome==""){
                      cout<<" -inserisci il nome del Cantante-Regista-Autore :\t";
                      getline(cin,autori[i].nome,'\n');
                     }
                     else break;
                 }while(true);
                 if(ce==false)break;
                 cout<<" -inserisci il cognome del Cantante-Regista-Autore :\t";
                 getline(cin,autori[i].cognome,'\n');
                 do{
                     if(autori[i].cognome==""){
                      cout<<" -inserisci il cognome del Cantante-Regista-Autore :\t";
                      getline(cin,autori[i].cognome,'\n');
                     }
                     else break;
                 }while(true);
                 do{
                     cout<<"-Inserire un altro Cantante-Regista-Autore ? (si= s . no= n) \t";
                     cin>>j;
                     cin.ignore();
                 }
                 while(j!="s" && j!="n");
                  i++;}
                 while(i<5);
                 cout<<"inserisci il titolo :\t";
                 getline(cin,titolo,'\n');
                 do{
                 if(titolo=="") {
                 cout<<"nessun titolo inserito"<<endl;
                 cout<<"inserisci il titolo :\t";
                 getline(cin,titolo,'\n');
                 }else break;

       }while(true);
                 cout<<"inserisci l'anno di pubblicazione"<<endl;
                 getline(cin,anno,'\n');
                 cout<<"inserisci la casa editrice:\t";
                 getline(cin,casaed,'\n');
                 contautori=i;
                 }
                 void get_titolo(string& a){
                 a=titolo;}

                 int get_autori(string a[]){
                                 if(contautori>=0)
                                 for(int i=0;i<contautori;i++)
                                 a[i]=autori[i].cognome;
                                 return contautori;
                                }

              void stampa_info(){

             if(contautori>=0){
                int i=0;

                do{
                   cout << "Nome:\t\t" << autori[i].nome << endl;
                   cout << "Cognome:\t" << autori[i].cognome << endl;
                   i++;
                }while(i<contautori);
                cout << "Titolo:\t\t" << titolo << endl;
                if(casaed!="")
                   cout << "Casa Editrice:\t" << casaed << endl;
                if(anno!="")
                   cout << "Anno:\t\t" << anno << endl;
             }
             }
              int get_contau (){
              return contautori;
              }
              string get_nome(int s){
              return autori[s].nome;
              }
              void scrivi_titolo(ostream& file1){
              file1<< titolo <<endl<< "\n";
              }
              void scrivi_info(ofstream& file1) {
              for(int i=0;i<contautori;i++){
              if(autori[i].nome!="")
              file1 << autori[i].nome <<"\n" << autori[i].cognome << "\n";
            }
            file1 <<"#\n" << titolo <<"\n";
            if(casaed!="") file1<<"*\n";
            else file1 << casaed<< "\n";
            if(anno=="") file1 << "0\n";
            else file1 << anno << "\n";
            file1.close();
        }
              bool leggi_da_file(char* name,ifstream& f2){
              int i=0;
              string canc,nome,cognome,casa_edit;
                
                do{
                    getline(f2,nome);
                    if(f2.eof()) return false;
                    if(nome!="#"){
                    getline(f2,cognome);
                    autori[i].nome=nome;
                    autori[i].cognome=cognome;
                   i++;
                   }if(i==5){ getline(f2,canc); break;}


             }while(nome!="#");

                autori[i].nome="";
                contautori=i;
                getline(f2,titolo);
                getline(f2,casa_edit);
                if(casa_edit=="*") casaed="";
                casaed=casa_edit;
                getline(f2,anno,'\n');
                if(anno=="0") anno="";

            return true;}


};

class libro: public pubblicazione{
             private:
             int n_pag;

             public:
             libro() {
             n_pag=0;
             }

             void aggiungi_libro() {
             aggiungi_infop();

            do{

             cout<<"inserisci il numero di pagine del libro :\t";
             cin>>n_pag;
             }while(n_pag==char('\n'));
             }

            void stampa_libro(){
                stampa_info();
                if(n_pag!=0)
                cout<<"- Numero pagine :\t "<<n_pag<<endl;
            }

            void scrivi_libro(ofstream& file1){
                 scrivi_info(file1);
                file1<<n_pag<<"\n";

                              }
            void leggilib(ifstream& f1){

                int n_pag;

               if(leggi_da_file("libri.txt",f1)==false) return;
                string num;
                getline(f1,num,'\n');
                n_pag=atoi(num.c_str());
                return;}
};

Last edited on
and this is set classes

#include "classe_persona.cpp"
#include "classe_pubblicazione.cpp"
#define max 100

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
class insieme_libri{

                private:
                 libro L[max];
                 int indice;

                 public:
                 insieme_libri(){
                 indice=0;
                 }
                 void aggiungi(){
                     if(indice!=max){
                     L[indice].aggiungi_libro();
                     indice++;
                     }else cout<<" !!! spazio insufficiente !!! "<<endl;

                 }
                     insieme_libri cancella(insieme_libri& origine,int x){
                     insieme_libri canc;
                     int i;
                     for(i=0;i<origine.indice-1;i++){

                     if (i<x) canc.L[i]=origine.L[i];
                     if(i>=x) canc.L[i]=origine.L[i+1];
                     }
                     canc.indice=i;
                     return canc;
                 }
                 void stampa(int x){
                     cout<< x+1<<"}"<<endl;
                     L[x].stampa_libro();
                     }

                void stampa_collezione(){
                for(int i=0;i<indice;i++)
                stampa(i);
                }
               void ricerca_nel_cognome(){
                    string s,f[5];
                    int x=0;
                    cout <<"inserisci una parte del cognome dell'autore:\t";
                    getline(cin,s,'\n');
                    for(int i=0;i<indice;i++){
                    x=L[i].get_autori(f);
                    if(x!=0){
                    for(int j=0;j<x;j++){
                    if((f[j].find(s,0))!=string::npos)
                    stampa(i);
                    }

                 }
             }
                }

                  void ricerca_nel_titolo(){
             string s,e;
             cout << "Inserisci parte del titolo :\t";
             getline(cin,e,'\n');
             for(int i=0;i<indice;i++){
                 L[i].get_titolo(s);
                 if((s.find(e,0))!=string::npos)
                  stampa(i);
             }
             if((s.find(e,0))==string::npos) cout << "\nessuna corrispondenza trovata" << endl;
        }

void scrivi_file(ofstream& file1,char* name){
          file1.open(name);
          for(int i=0;i<indice;i++){
          L[i].scrivi_libro(file1);
          }
          file1.close();
        }
        void scrivi_title(ostream& file1,int x){
             L[x].scrivi_titolo(file1);
        }
        void leggi_insieme_l(){
          ifstream f;
          f.open("libri.txt");
          if(!f.fail()){
              do{
                 if(f.eof()) break;
                 L[indice].leggilib(f);
                 if(L[indice].get_nome(L[indice].get_contau())=="") return;
                 indice++;
              }while(true);
              f.close();
              return;
          }
        }

        };

class insieme_musica{
      private:
         musica M[max];
         int ind;
      public:
        insieme_musica(){
               ind=0;
        }
        void aggiungi(){
             if(ind!=max){
             M[ind].aggiungi_musica();
             ind++;
             }
        }
        insieme_musica cancella(insieme_musica& orig,int x){
            insieme_musica canc;
            int i;
            for(i=0;i<orig.ind-1;i++){
                if(i<x) canc.M[i]=orig.M[i];
                if(i>=x) canc.M[i]=orig.M[i+1];
            }
            canc.ind=i;
            return canc;
        }
        void stampa(int x){
             cout << x+1 << ")" << endl;
             M[x].stampa_brani_con_durata();
        }
        void stampa_cd(){
             for(int i=0;i<ind;i++){
                 stampa(i);
                 cout <<endl;
             }
        }
        void ricerca_nel_titolo(){
             string s,e;
             cout << "Inserisci parte del titolo dell'album:\t";
             getline(cin,e,'\n');
             for(int i=0;i<ind;i++){
                 M[i].get_titolo(s);
                 if((s.find(e,0))!=string::npos)
                  stampa(i);
             }
             if((s.find(e,0))==string::npos) cout << "\ncorrispondenza non trovata" << endl;
        }
        void ricerca_nel_cognome(){
        string s,f[5];
              int x=0;
              cout <<"inserisci una parte del cognome del cantante:\t";
              getline(cin,s,'\n');
              for(int i=0;i<ind;i++){
                  x=M[i].get_autori(f);
                  if(x!=0){
                     for(int j=0;j<x;j++){
                           if((f[j].find(s,0))!=string::npos)
                               stampa(i);
                     }
                  }
              }
        }
        void scrivi_file(ofstream& file1,char* name){
          file1.open(name);
          for(int i=0;i<ind;i++){
              M[i].scrivi_musica(file1);
          }
          file1.close();
        }
        void scrivi_title(ostream& file1,int x){
             M[x].scrivi_titolo(file1);
        }
        void leggi_insieme_m(){
             ifstream f1;
             f1.open("musica.txt");
             if(!f1.fail()){
               do{
                  if(f1.eof()) break;
                  M[ind].leggi_a(f1);
                  if(M[ind].get_nome(M[ind].get_contau())=="") return;
                  ind++;
               }while(true);
               f1.close();
               return;
             }
        }
};

class insieme_film{
      private:
         film F[max];
         int index;
      public:
        insieme_film(){
               index=0;
        }
        void aggiungi(){
             if(index!=max){
             F[index].aggiungi_film();
             index++;
             }
        }
        insieme_film cancella(insieme_film& orig,int x){
            insieme_film canc;
            int i;
            for(i=0;i<orig.index-1;i++){
                if(i<x) canc.F[i]=orig.F[i];
                if(i>=x) canc.F[i]=orig.F[i+1];
            }
            canc.index=i;
            return canc;
        }
        void stampa(int x){
             cout << x+1 << ")" << endl;
             F[x].stampa_film();
        }
        void stampa_film(){
             for(int i=0;i<index;i++){
                 stampa(i);
                 cout <<endl;
             }
        }
        void ricerca_nel_titolo(){
             string s,e;
             cout << "Inserisci parte del titolo del film:\t";
             getline(cin,e,'\n');
             for(int i=0;i<index;i++){
                 F[i].get_titolo(s);
                 if((s.find(e,0))!=string::npos)
                  stampa(i);
             }
             if((s.find(e,0))==string::npos) cout << "\ncorrispondenza non trovata" << endl;
        }
        void ricerca_nel_cognome(){
        string s,f[5];
              int x=0;
              cout <<"inserisci una parte del cognome del cantante:\t";
              getline(cin,s,'\n');
              for(int i=0;i<index;i++){
                  x=F[i].get_autori(f);
                  if(x!=0){
                     for(int j=0;j<x;j++){
                           if((f[j].find(s,0))!=string::npos)
                               stampa(i);
                     }
                  }
              }
        }



        void scrivi_file(ofstream& file1,char* name){
          file1.open(name);
          for(int i=0;i<index;i++){
              F[i].scrivi_film(file1);
          }
          file1.close();
        }
        void scrivi_title(ostream& file1,int x){
             F[x].scrivi_titolo(file1);
        }
        void leggi_insieme_f(){
             ifstream f1;
             f1.open("film.txt");
             if(!f1.fail()){
               do{
                  if(f1.eof()) break;
                  F[index].leggi_f(f1);
                  if(F[index].get_nome(F[index].get_contau())=="") return;
                  index++;
               }while(true);
               f1.close();
               return;
             }
        }
};
Topic archived. No new replies allowed.