-1

If i read a picture with fread and imediately write all readed data into a file the original file (picture) differs completely from newly written file. As written in the topic these two files differ completely. I do compare it with hexdump. Would there be please some one kind and help me? The same problem i have when i read code from FCGI_getchar. The original file and the new file differ in hexdump comparison.

#include "fcgi_stdio.h"
#include <stdlib.h>
#include <string.h>

int main(void)
{
    int count = 0;
//  char stdinchar;
    char *requestmethod;
    FILE *fptr; 
    FILE *fptr1;
    char *buffer;
    long size;


    while(FCGI_Accept() >= 0) {

        printf("Content-type: text/html\r\n"
        "\r\n"
        "<title>FastCGI Hello!</title>"
        "<h1>FastCGI Hello!</h1>"
        "<p>das ist ein test</p>"
        "Request number %d running on host <i>%s</i>\n",
        ++count, getenv("SERVER_NAME")); 
    
        printf("<form action=\"fcgid-script.fcgi\" method=\"post\" enctype=\"multipart/form-data\">");
        printf("    <label>Wählen Sie eine Datei.");
        printf("        <input name=\"datei\" type=\"file\">"); 
        printf("    </label>");  
        printf("    <button>… und ab geht die Post!</button>");
        printf("</form>\n\n");
    
/*      while ( stdinchar != 0 ) { */
/*      while (stdinchar) { */
        requestmethod = getenv("REQUEST_METHOD");
/*      fptr = fopen("/var/www/hiawatha/picture.jpg","wb"); */

        if (strcmp( requestmethod, "POST") == 0) {
    
/*          printf("<i>content-length:%s</i><i>content-type:%s</i>\n",
            getenv("CONTENT_LENGTH"), getenv("CONTENT_TYPE"));      
            while (FCGI_getchar() != EOF ) { 
*/  
    
            fptr = fopen("/home/josef/Pictures/1925.jpg", "rb");
            fptr1 = fopen("/home/josef/Pictures/test123.jpg", "w+b");
            fseek (fptr , 0 , SEEK_END);
            size = ftell(fptr);
            rewind(fptr);   

//          while (!feof(fptr)) {

            fread(buffer, 1, size, fptr);
            fwrite(buffer, 1, size, fptr1);

            fclose(fptr);
            fclose(fptr1);
//      }
    
//          stdinchar = (char) FCGI_getchar(); 
        
//          printf("%c", stdinchar); 
/*          fprintf(fptr,"%d",stdinchar); */
/*          fwrite(&stdinchar, 1, sizeof(stdinchar), fptr); */
        
    
/*          printf("<p>das ist ein test</>"); */

//      }

/*      fclose(fptr);  */   
    
        } 
    }    

    return 0;
}
  • 2
    There's a lot of out commented code, and also some code that seems to be redundant. Please create a [mre] – klutt Jul 12 '20 at 01:10
  • 1
    Also, you might take a look at [why while(!feof()) is always wrong](https://stackoverflow.com/q/5431941/6699433) – klutt Jul 12 '20 at 01:11

1 Answers1

2

Certainly one problem here is that you're reading a file into the memory pointed to by buffer, but that is an uninitialized pointer--- you haven't allocated any actual memory for the buffer, so you're reading a file and copying those bytes into some random memory, which is bad. I'm impressed that it seems runs to completion without crashing outright. But in any case, you need to:

buffer = malloc(size); 

in order to allocate the actual buffer space, before doing the fread. Then make sure you free(buffer); later when you're done using it. That may or may not fix any other issues you have here but is necessary.

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
  • 2
    Good response, I'd just get rid of the `* sizeof(char)` bit, that's guaranteed to *always* be one. – paxdiablo Jul 12 '20 at 01:16
  • 1
    What @paxdiablo said, plus that it's much better to write `ptr = malloc(size * sizeof *ptr)` so that you always get the type correct – klutt Jul 12 '20 at 01:20
  • @paxdiablo Fair! I write the `* sizeof(char)` as a long-standing stylistic tic for parallelism with buffers of other object sizes (and also bc I started before C99. :) ). But I do agree that it's idiomatic now to do without. I'm editing the answer and leaving this. Thanks. – Ben Zotto Jul 12 '20 at 15:56
  • @BenZotto it has *always* been idiomatic, as `sizeof(char)==1` has always been true. – mlp Jul 12 '20 at 17:00