c++ - What's making this algorithm crash? -


before showing code, let me explain problem in detail. need create 2 dat files data hotels in them. 1 of files contains following info: name of province hotel in (string of 16), category of hotel (int), code of hotel (int), name of hotel (string of 30), amount of rooms (int), , amount of reserved rooms (int). second dat file contains code of hotels being eliminated list. final part of test create file has of information of remaining hotels. since, in theory, have info if 2 first dat files, wrote simple. noteworthy mention have use dev c++ 4.9.9.2:

#include <iostream> #include <cmath> #include <fstream> #include <string.h> #include <cstdio>  using namespace std;   struct rhot {          char provincia[16];    int categoria;    int chot;    char nombrehotel[30];    int canthabitaciones;    int habitreservadas; };  int main() { rhot hot1 = {      "còrdoba",  4,  1,  "la pachamama",  100,  49  }; rhot hot2 = {      "salta",  3,  2,  "puntoblanco",  60,  13  }; rhot hot3 = {      "catamarca",  4,  3,  "jaguar resort",  250,  197  }; rhot hot4 = {      "chubut",  6,  4,  "llao llao",  300,  299  }; rhot hot5 = {      "la pampa",  2,  5,  "gaunchito",  40,  20  }; rhot hot6 = {      "mendoza",  3,  6,  "queseyo",  60,  13  };   file* f1;  f1 = fopen("arhot.dat", "w");  fwrite(&hot1,sizeof(hot1),1,f1);  fwrite(&hot2,sizeof(hot2),1,f1);  fwrite(&hot3,sizeof(hot3),1,f1);  fwrite(&hot4,sizeof(hot4),1,f1);  fwrite(&hot5,sizeof(hot5),1,f1);  fwrite(&hot6,sizeof(hot6),1,f1);   fclose(f1);    system("pause"); return exit_success; } 

and second one:

 #include <iostream>  #include <cmath>  #include <fstream>  #include <string.h>  #include <cstdio>  using namespace std;   struct rbaja {          int cbajas;  };  int main() { rbaja baja1 = {      1  }; rbaja baja2 = {      5  };    file* f1; f1 = fopen("arbaj.dat", "w");  fwrite(&baja1,sizeof(baja1),1,f1);  fwrite(&baja2,sizeof(baja2),1,f1);    fclose(f1);    system("pause"); return exit_success; } 

(i want mention dont know libraries need this, tried deleting each 1 of them , running programs , isnt giving me errors when has no libraries, because dont know left them there)

and i'm trying make work. need read 2 files created above, , try create 1 based on said @ start. barely wrote fread function , whenever compile , run it crashes

#include <iostream> #include <cmath> #include <fstream> #include <string.h> #include <cstdio> #include <cstdlib>   using namespace std;  file* filearhot;  file* filearbaj;  file* filearact;  struct rhot {              char provincia[16];        int catergoria;        int chot;        char nombrehotel[30];        int cantahibaticones;        int habitreservadas; };  struct rbaja {              int cbajas; };  struct rhotact {              char provincia[16];        int catergoria;        int chot;        char nombrehotel[30];        int cantahibaticones;        int habitreservadas; };  int main() { filearhot =fopen("arhot.dat","r"); filearbaj =fopen("arbaj.dat","r"); filearact =fopen("aract.dat","w");  fread(&filearhot,sizeof(rhot),1,filearhot); fread(&filearbaj,sizeof(rbaja),1,filearbaj);    fclose(filearhot);  fclose(filearbaj);  fclose(filearact);   system("pause");  return exit_success; } 

whenever try run this, program crashes. think because doing infinite loop or something,but have no idea. included creation code of 2 dat files because might have created them wrong.

either way, :d (also sorry if text in spanish, i'm argentina , need code spanish words.)

you're passing wrong arguments fread;

fread(&filearhot,sizeof(rhot),1,filearhot); 

overwrites filearhot, , undefined.

you forgot declare variables intend read into.

it should be

rhot an_rhot; // or whatever want name fread(&an_rhot, sizeof(rhot), 1, filearhot); 

or safer

fread(&an_rhot, sizeof(an_rhot), 1, filearhot); 

and similar other read.

you should add checking verify you've opened, read, , written files successfully.

i recommend @ using c++ i/o instead of c libraries, it's safer.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -