00001
00002
00003 #include "file.h"
00004
00011 void fileCrea(const char* nomeFile){
00012 ofstream daCreare;
00013 daCreare.open(nomeFile, ios::app);
00014 if (daCreare.fail()){
00015 cout << "Impossibile creare il file. Disco pieno?" << endl;
00016 exit(1);
00017 }
00018 daCreare.close();
00019 }
00020
00028 bool fileVuoto(const char* nomeFile){
00029 ifstream daContare;
00030 daContare.open(nomeFile);
00031 daContare.seekg(0, ios::end);
00032 int lunghezza = daContare.tellg();
00033 daContare.close();
00034 return lunghezza==0;
00035 }
00036
00046 bool filePrimoCampo(const char* nomeFile){
00047 if (fileVuoto(nomeFile)){
00048 return true;
00049 }
00050
00051 ifstream daControllare;
00052 daControllare.open(nomeFile);
00053 daControllare.seekg( -1, ios::end);
00054 char precedente = daControllare.get();
00055 daControllare.close();
00056 if (precedente == '\n'){
00057 return true;
00058 }
00059
00060 return false;
00061 }
00062
00070 int fileContaRecordInt(ifstream* file){
00071 int record = 0;
00072 file->seekg(0, ios::beg);
00073 while (!file->eof()){
00074 char c = file->get();
00075 if (c == '\n'){
00076 record++;
00077 }
00078 }
00079
00080
00081 file->seekg(0, ios::beg);
00082 file->clear();
00083 return record;
00084 }
00085
00098 void filePosizionaRecord(ifstream* file, int y){
00099 file->seekg(0, ios::beg);
00100
00101 for (int i = 0; i < y; i++){
00102 char c = 'a';
00103 while ((c != '\n') && (!file->eof())){
00104 c = file->get();
00105 }
00106 }
00107 }
00108
00124 void filePosizionaCampo(ifstream* file, int x, int y){
00125 filePosizionaRecord(file, y);
00126
00127 for (int i = 0; i < x; i++){
00128 char c = 'a';
00129 while (c != ','){
00130 c = file->get();
00131 }
00132 }
00133 }
00134
00144 int lunghezzaProssimoCampo(ifstream* file){
00145 int risultato = 0;
00146
00147 char c = file->get();
00148 while (c != ',' && c != '\n'){
00149 risultato++;
00150 c = file->get();
00151 }
00152
00153 return risultato;
00154 }
00155
00156
00157
00158
00159
00160 void fileAggiungiCampo(const char* nomeFile, const char* s){
00161 if (!fileEsiste(nomeFile)){
00162 fileCrea(nomeFile);
00163 }
00164
00165 bool primo = filePrimoCampo(nomeFile);
00166
00167 ofstream fileScrivi;
00168 fileScrivi.open(nomeFile, ios::app);
00169 if (!primo){
00170 fileScrivi << ",";
00171 }
00172 fileScrivi << s;
00173 fileScrivi.close();
00174 }
00175
00176 void fileAggiungiCampoUltimoChar(const char* nomeFile, const char* s){
00177 if (!fileEsiste(nomeFile)){
00178 fileCrea(nomeFile);
00179 fileAggiungiCampo(nomeFile, s);
00180 }
00181 else {
00182 filebuf fileScrivi;
00183 ostream output(&fileScrivi);
00184 istream input(&fileScrivi);
00185 fileScrivi.open (nomeFile, ios::in | ios::out);
00186
00187 output.seekp( -1, ios_base::end);
00188 output << s;
00189 fileScrivi.close();
00190 }
00191 }
00192
00193 void fileNuovoRecord(const char* nomeFile){
00194 if (!fileEsiste(nomeFile)){
00195 fileCrea(nomeFile);
00196 }
00197
00198 ofstream fileScrivi;
00199 fileScrivi.open(nomeFile, ios::app);
00200 fileScrivi << endl;
00201 fileScrivi.close();
00202 }
00203
00204 int fileContaRecord(const char* nomeFile){
00205 ifstream file;
00206 file.open(nomeFile);
00207 int risultato = fileContaRecordInt(&file);
00208 file.close();
00209 return risultato;
00210 }
00211
00212 bool fileEsiste(const char* nomeFile){
00213 ifstream daControllare;
00214 daControllare.open(nomeFile);
00215 if (daControllare.fail()){
00216 return false;
00217 }
00218 else {
00219 daControllare.close();
00220 return true;
00221 }
00222 }
00223
00224 char fileUltimoCarattere(const char* nomeFile){
00225 ifstream file;
00226 file.open(nomeFile);
00227 file.seekg( -1, ios::end);
00228 char risultato = file.get();
00229 file.close();
00230 return risultato;
00231 }
00232
00233 char* fileLeggiCampo(const char* nomeFile, int x, int y){
00234 ifstream file;
00235 file.open(nomeFile);
00236
00237 filePosizionaCampo(&file, x, y);
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251 int lunghezza = lunghezzaProssimoCampo(&file);
00252 char* risultato = new char[lunghezza + 1];
00253
00254 filePosizionaCampo(&file, x, y);
00255
00256 for (int i = 0;i < lunghezza;i++){
00257 risultato[i] = file.get();
00258 }
00259 risultato[lunghezza] = '\0';
00260
00261 file.close();
00262 return risultato;
00263 }