0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char *cognome=NULL;
    char *nome=NULL;   
    char *email=NULL;
    char *password=NULL;
    char *password2=NULL;
    FILE *file_utenti=NULL;
    file_utenti=fopen("Utenti.dat","a+");
    struct utente 
    {
        char cognome[25];
        char nome[25];
        char email[80];
        char password[64];
        char password2[64];
    };
    struct utente Utente;
    file_utenti=fopen("Utenti.dat","r"); 
    if(file_utenti!=NULL)
    printf("%s\n","File aperto correttamente in lettura");
    else
    printf("%s\n","Impossibile leggere sul file utenti");
    while(fread(&Utente, sizeof(Utente), 1, file_utenti))
    {
        printf("%s\t",Utente.cognome);
        printf("%s\t",Utente.nome);
        printf("%s\t",Utente.email);
        printf("%s\t",Utente.password);
        printf("%s\t",Utente.password2);
        printf("\n");
    }

    fclose(file_utenti);
    return 0; 
}

If I run it as a cgi script, it doesn't enter the while, but it works perfectly if I run it in /var/www/cgi-bin/ directory. It opens the file, print all the records, and then quits Of course, I used html tag in my cgi script. I mean, I used a table to show data from the file But it only writes the tag table

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
  • 1
    Translating everything to English would help the code to make the question easier to understand. – Yunnosch Nov 26 '18 at 20:23
  • 1
    You attempt the while loop whether or not the file opening succeeded, this kind of check, when failing should prevent further processing. – Yunnosch Nov 26 '18 at 20:27
  • why is the struct definition inside the `main()` function rather than outside any function? – user3629249 Nov 26 '18 at 23:34
  • @user3629249 Why not, if it is nowhere else needed? Do you have concerns about this? – Scheff's Cat Nov 27 '18 at 06:53
  • @user3629249 Your comment made me a bit uncertain whether it's allowed by standard. ;-) So, I googled and found [SO: Local struct in c](https://stackoverflow.com/a/11016595/7478597). By standard, it should be OK. Do you have concerns about style? – Scheff's Cat Nov 27 '18 at 06:56
  • the linked answer is about not using a tag name in a struct definition, NOT about the advisability of embedding the struct definition inside the scope of a single function. Most debuggers will not display the contents of a struct, field by field, unless a tag name is included in the definition – user3629249 Nov 27 '18 at 07:35
  • regarding: `printf("%s\n","Impossibile leggere sul file utenti");` error messages should be output to `stderr`, not `stdout`. When the error indication is from a C library function, then should also output to `stderr` the text reason the OS thinks the error occurred. Calling `perror( "your error message" );` does all of the above. when a file fails to open, do NOT be trying to read from it. rather the next statement should be: `exit( EXIT_FAILURE );` – user3629249 Nov 27 '18 at 07:42
  • these statements ` char *cognome=NULL; char *nome=NULL; char *email=NULL; char *password=NULL; char *password2=NULL;` are not used, so should be removed from the posted code – user3629249 Nov 27 '18 at 07:47
  • this statement: `file_utenti=fopen("Utenti.dat","a+");` should be removed. The FILE* file_utenti is overlayed by the second call to `fopen()` – user3629249 Nov 27 '18 at 07:52
  • I apologize for my mistakes, I was in a hurry. But the program works perfctly if I run it as a standalone script in /var/www/cgi-bin/, but it seems not entering in the while loop when executed as a cgi script in the browser. Of course I used to print the content-type: text/html as first string to output to stdout, followed by html tags, but it only prints before entering in the while loop
    – Francesco Di Fusco Nov 27 '18 at 14:43
  • where does ' come from? Is it inside the input file? Please post a [mcve], including the contents of the input flie
    – user3629249 Nov 27 '18 at 18:00
  • this cgi script read data from a text file and show them using an html table, – Francesco Di Fusco Nov 27 '18 at 18:55

1 Answers1

0

The following proposed code:

  1. cleanly compiles
  2. performs the desired functionality
  3. contains the suggested modifications in the comments to the question
  4. it is a poor programming practice to include header files those contents are not used in the code. I.E. Suggest removing the statement: #include <string.h>

And now, the proposed code:

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

int main()
{  
    struct utente 
    {
        char cognome[25];
        char nome[25];
        char email[80];
        char password[64];
        char password2[64];
    };
    struct utente Utente;

    FILE *file_utenti=fopen("Utenti.dat","r"); 

    if( file_utenti )
        printf("%s\n","File aperto correttamente in lettura");

    else
    { // fopoen failed
        perror("Impossibile leggere sul file utenti");
        exit( EXIT_FAILURE );
    }

    while(fread( &Utente, sizeof(Utente), 1, file_utenti) )
    {
        printf("%s\t",Utente.cognome);
        printf("%s\t",Utente.nome);
        printf("%s\t",Utente.email);
        printf("%s\t",Utente.password);
        printf("%s\t",Utente.password2);
        printf("\n");
    }

    fclose(file_utenti);
    return 0; 
}

However, the question does not clarify if each string is terminated via a NUL byte. If not terminated by a NUL byte, then the code will output garbage.

user3629249
  • 16,402
  • 1
  • 16
  • 17