Thread: Why this code wrote empty file?

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    51

    Why this code wrote empty file?

    Code:
    #include <fcgi_stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    
    int main(void)
    {
    
        int count = 0;
        while(FCGI_Accept() >= 0) {
        
    
            char *contentLength = getenv("CONTENT_LENGTH");
            int len;
    
            if (contentLength != NULL) {
                len = strtol(contentLength, NULL, 10);            
            }
            else {
                len = 0;
            }
            printf("Content-type: text/html\r\n"
                   "\r\n"
                   "<title>FastCGI Hello!</title>"
                   "<h1>FastCGI Hello!</h1>"
                   "Request number %d running on host <i>%s</i>\n",
                    ++count, getenv("SERVER_NAME"));
    
            printf("<br />CONTENT_LENGTH = %d <br />\r\n", len);
            printf("<form enctype='multipart/form-data' method='post' action='?'><input type='text' name='text1' /><input type='file' name='file1'/><input type='submit' /></form>");
            printf("<hr />");
    
            fflush(stdout);
    
            FCGI_FILE * fileOut = FCGI_fopen("/tmp/fcgi.out", "w");
            if (fileOut) {
                int done = 0;
                while(done < len) {
                    char buffer[1024];
                 
                    int packetRead;
    
                    packetRead = FCGI_fread(buffer, 1, sizeof(buffer), stdin);
                    if (packetRead < 0) {
                        break;
                    }
                    if (packetRead > 0) {
                        FCGI_fwrite(buffer, 1, packetRead, fileOut);
                        done += packetRead;
                    }
    
    
                }
                FCGI_fclose(fileOut);
            }
    
            FCGI_Finish();
        }    
    }
    In this code sample fcgi2 library is used. After click on page button file fcgi.out was empty. Why?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if (packetRead < 0)
    This will get you out of the loop without doing anything.

    Perhaps add some diagnostics to your code - rather than assume success and posting on a forum when it doesn't work.
    Code:
                    packetRead = FCGI_fread(buffer, 1, sizeof(buffer), stdin);
                    if (packetRead < 0) {
                        fprintf(stderr,"FCGI_fread returned %d\n", packetRead);
                        break;
                    }

    > ++count, getenv("SERVER_NAME"));
    The count will be zero every time you run a new copy of this program.
    So if the server is really expecting some incremented number, this isn't going to work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-16-2020, 10:44 AM
  2. Replies: 8
    Last Post: 09-18-2018, 11:26 PM
  3. Replies: 30
    Last Post: 12-11-2017, 12:26 PM
  4. How to more intuitively wrote more elegant c++ code?
    By Tesp in forum C++ Programming
    Replies: 13
    Last Post: 07-26-2016, 03:58 PM
  5. I wrote a piece of code, can't find where is the bug....
    By meili100 in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2007, 11:25 AM

Tags for this Thread