D
D
Dreaded2017-10-17 23:16:09
C++ / C#
Dreaded, 2017-10-17 23:16:09

Working with streams in SI. How to copy information to a file?

There is the following C code that works with "bmp" files.
In general, the essence of the code is such that I will copy the contents of one bmp file byte by byte to another, and work with the copied values ​​in the new file.
The problem is that when I copy the data from the old file to the new one, and access it in the printf function, the value 0 is returned in the new file. It's like nothing was copied.
It seems to me that I simply do not correctly understand the principle of operation of these functions, and I am doing something wrong. Tell me how to correctly copy the data from the new file to the old one, and change them in the future? :(

char *infile = argv[1];
    char *outfile = argv[2];

    // open input file
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

    // open output file
    FILE *outptr = fopen(outfile, "w");
    if (outptr == NULL)
    {
        fclose(inptr);
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 3;
    }
    // read infile's BITMAPFILEHEADER
    BITMAPFILEHEADER bf;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

    // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
    if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
        bi.biBitCount != 24 || bi.biCompression != 0)
    {
        fclose(outptr);
        fclose(inptr);
        fprintf(stderr, "Unsupported file format.\n");
        return 4;
    }

    // Передаю в функцию адрес  BITMAPFILEHEADER исходного файла, что бы скопировать его в новый файл
    fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

    // Передаю в функцию адрес BITMAPINFOHEADER исходного файла, что бы скопировать его в новый файл
    fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

     //перевожу указатель в потоке нового файла на самое начало    
     fseek(outptr, 0, SEEK_SET);
     
    //считываю в новом файле BITMAPFILEHEADER в  &bf_new
    BITMAPFILEHEADER bf_new;
    fread(&bf_new, sizeof(BITMAPFILEHEADER), 1, outptr);

    // считываю в новом файле BITMAPINFOHEADER в &bi_new
    BITMAPINFOHEADER bi_new;
    fread(&bi_new, sizeof(BITMAPINFOHEADER), 1, outptr);


    //Пытаюсь обратится  к параметрам исходного файла и нового файла. НЕ ПОЛУЧАЕТСЯ :'(
    printf("biWidth of %s = > %i \n", infile, bi.biWidth);   //выдает корректное значение исходного файла
    printf("biWidth of %s = > %i \n ", outfile, bi_new.biWidth); //выдаёт 0, хотя должно выдавать точно такое же как и выше.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
15432, 2017-10-17
@Dreaded

1) open non-text files with "rb" and "wb" respectively
2) for writing, set size to 1 and count to sizeof(...), so you know exactly how many bytes are written in the return value
3) if you're going to not only write, but also read, open the file not by "wb", but by "w + b"
4) check the return value everywhere to make sure that the function worked. You probably have the latest fread failed
5) why read / write to slow files? Work with data directly in memory. new/malloc and go

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question