Thread: How can I download an Image from the web

  1. #16
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Heh, I know this is flawed and something like that would ruin the program at runtime. I could write it to be more robust but at the moment, I would like to use a block reading function instead of reading and writing a byte at a time. I could start at the end of the filename and then search for the first instance of '.' that is an option.

  2. #17
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You haven't shown us the block reading/writing function that you said doesn't work.

  3. #18
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Code:
        
        while((n = fread(buffer,1,sizeof(buffer),ifile)) && nread < file_size){
        
            buffer[n] = 0;
            fwrite(buffer,1,strlen(buffer),ofile);
            nread += n;        
        }
    It will copy text-based files but with garbage at the bottom. Image files dont copy completely. With my other method, using fgetc and fputc I can copy video, images, sources, audio and it can be all be played back.

  4. #19
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    If you're working with binary data, you do not want to 0-terminate the buffer, you just want to copy whatever you read right back out to the output stream. And you therefore don't want to use strlen on the output length side of things either.

    A full example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        const char *input = "ohface.gif";
        const char *output = "ohface_copy.gif";
    
        FILE *in = fopen(input, "rb");
        if (!in)
        {
            printf("Could not open %s for reading\n", input);
            return 1;
        }
    
        FILE *out = fopen(output, "wb");
        if (!out)
        {
            fclose(in);
            printf("Could not open %s for writing\n", output);
            return 1;
        }
    
        char buf[BUFSIZ] = { 0 };
        size_t numRead = 0;
        while ((numRead = fread(buf, 1, sizeof(buf), in)) != 0)
        {
            if (numRead == -1)
            {
                perror("Read error");
                fclose(out);
                fclose(in);
                return 1;
            }
    
            if (fwrite(buf, 1, numRead, out) != numRead)
            {
                perror("Write error");
                fclose(out);
                fclose(in);
                return 1;
            }
        }
    
        fclose(out);
        fclose(in);
        return 0;
    }
    Code:
    ls -l *.gif
    -rw-r--r--  1 rags  staff  82445 Sep 18 15:14 ohface.gif
    -rw-r--r--  1 rags  staff  82445 Sep 18 15:25 ohface_copy.gif
    Last edited by rags_to_riches; 09-18-2010 at 01:27 PM.

  5. #20
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    The null termination and strlen parts were the problem. It works perfectly now. You guys are freaking programming demigods haha.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. capturing image from web cam
    By sravanthidesu25 in forum C++ Programming
    Replies: 1
    Last Post: 03-16-2010, 10:43 AM
  2. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 04:17 AM
  3. HotSpot image controls (web)
    By novacain in forum C# Programming
    Replies: 0
    Last Post: 06-25-2008, 04:27 AM
  4. download free bsd image?
    By iain in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-11-2002, 10:10 AM