• C

Converting image file into strings

Hi, i got a question here is tat I wanna convert a image file(e.g. JPEG, bmp) into Strings and save them into the buffer, Do u guys the code i need to achieve this?? thz a lot!!
jeonlineAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tim_UtschigCommented:
You just want to read an entire file into a buffer?


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

long fsize(FILE *fp) {
    /* is there an easier standards-compliant (including working on winders) way to get file size? */

    long origpos, filesize;

    if((origpos = ftell(fp)) == -1)
        return -1;

    if(fseek(fp, 0, SEEK_END) != 0)
        return -1;

    if((filesize = ftell(fp)) == -1)
        return -1;

    if(fseek(fp, origpos, SEEK_SET) != 0)
        return -1;

    return filesize;
}

int main(int argc, char *argv[]) {
    FILE *fp;
    char *buffer;
    long filesize;

    if(argc != 2) {
        fprintf(stderr, "Usage: %s filename\n", argv[0]);
        return 1;
    }

    fp = fopen(argv[1], "r");
    if(!fp) {
        perror("fopen");
        return 1;
    }

    filesize = fsize(fp);
    if(filesize == -1) {
        perror("fsize");
        return 1;
    }

    buffer = (char*)malloc(filesize);
    fread(buffer, filesize, 1, fp);
    if(ferror(fp)) {
        perror("fread");
        return 1;
    }

    fclose(fp);

    /* do whatever you want with buffer */

    return 0;
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
grg99Commented:
How exactly do you want to convert an image into strings?

You mean convert white pixels to  spaces and dark pixcels to asterisks?


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.