-1
#include <stdio.h>
#include <conio.h> 
#include <stdlib.h>
#include <time.h>
typedef struct 
{
  int Id_provincia;
  struct fec{
      int dd;
      int mm;
      int aa;
  }Fecha;
  char Tipo_alerta;
}incendio;


int main(){
    int Num;
    FILE *archivo;
    archivo = fopen("Incendios_carga.dat", "rb");
    if (archivo == NULL){
        exit(1);
    }

    incendio foco;
    fread(&foco, sizeof(incendio), 1, archivo);
    while(!feof(archivo)){
        printf("el ID_Proviconcia es: : %d \n",foco.Id_provincia);
        fread(&foco, sizeof(incendio), 1, archivo);
        Num++;
    }
    printf("la cantidad de regs son:%d",Num);
    fclose(archivo);
}

this program should show on the screen the Id_Province that are 22 and it does not do that it shows random numbers I don't know why. The file that I am opening comes from another program with which I load the information to the file to process it with this program

  • 3
    How was the file you read from created? What was actually written to it? – Some programmer dude Nov 01 '22 at 14:21
  • 3
    Note that [`while (!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong)! – Jonathan Leffler Nov 01 '22 at 14:22
  • 2
    Also, note that `Num` is never initialized, so you will get indeterminate (quasi-random) values in it – Jonathan Leffler Nov 01 '22 at 14:22
  • @JonathanLeffler, note that this particular code happens to rescue the `while(!feof(archivo))` by hoisting one `fread()` out of the loop. Not that I think that's good style, but it's not semantically wrong. – John Bollinger Nov 01 '22 at 15:27
  • @JohnBollinger — yes, I noticed that the "always" in "always wrong" wasn't "completely accurate". However, it does involve unnecessary code repetition. The loop should be `while (fread(&foco, sizeof(incendio), 1, archive) == 1)` without any other call to `fread()` around. And then you don't need to check `feof()` either. – Jonathan Leffler Nov 01 '22 at 16:19

1 Answers1

0

Your primary problem is that you do not initialize Num to zero, so you get indeterminate (quasi-random) starting points for your counting.

You also have unnecessary includes, use feof() unnecessarily, and have unnecessary code repetition because of that. (See also the discussion in the comments to the question.)

Your code should be more like this:

#include <stdio.h>
#include <stdlib.h>

typedef struct 
{
    int Id_provincia;
    struct fec
    {
        int dd;
        int mm;
        int aa;
    } Fecha;
    char Tipo_alerta;
} incendio;

int main(void)
{
    const char filename[] = "Incendios_carga.dat";
    FILE *archivo = fopen(filename, "rb");
    if (archivo == NULL)
    {
        fprintf(stderr, "failed to open file '%s' for reading\n", filename);
        exit(1);
    }

    incendio foco;
    int Num = 0;
    while (fread(&foco, sizeof(foco), 1, archive) == 1)
    {
        printf("el ID_Proviconcia es: %d\n", foco.Id_provincia);
        Num++;
    }
    printf("la cantidad de regs son: %d\n", Num);
    fclose(archive);
    return 0;
}

The placement of braces is subjective — you can use 1TBS, but I prefer Allman. Just be consistent (and you were almost entirely consistent — the { after the typedef is on the wrong line for 00% consistency).

Print messages with a trailing newline. Print errors on stderr, not stdout. Avoid exiting a program silently when an error occurs. Report the name of the file that can't be opened. That usually means "use a variable (or #define) to hold the filename so you can pass it to both fopen() and fprintf()". IMO, it is rarely correct to call fopen() or any other file-opening function with a string literal for the file name because you need to repeat the string in the error message. Since you defined variable foco just before the loop, I move the definition of Num there too, initializing it to zero. I prefer programs to finish with return 0; even though C99 says it isn't necessary. (I regard that as a bug in the standard, but it was done for conformity with C++98. I just don't use the (mis-)feature.)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278