将无效参数传递给认为无效参数致命的函数

时间:2018-04-09 10:42:03

标签: c++

我希望你能帮助我。我有这个功能,我用来保存位图图像,但我有文件名的问题。我想用文件名保存当前日期的图像,但我现在是怎么做的,它给了我“一个无效参数被传递给一个认为无效参数致命的函数”错误并且程序崩溃。

以下是从主程序中获取名称的函数:

void DoSave(char *Name)
{
    time_t rawTime;
    struct tm *timeInfo;
    char buffer[100];
    string date;

    time(&rawTime);
    timeInfo = localtime(&rawTime);

    strftime(buffer, 100, " %c", timeInfo);
    date = buffer;

    string url("Images/");
    string name(Name);
    string extension(".bmp");

    string path = url + name + date + extension;

    // Open the file for writing
    FILE *f = fopen((const char*)&path, "wb");

    // Save the image as bmp file
    BmpHelper::SaveTo8bppBmpFile(f, (LONG)m_ImageSizeX, (LONG)m_ImageSizeY, (unsigned char*)videoImage);
    fclose(f);
}

这是保存位图的功能

void BmpHelper::SaveTo8bppBmpFile(FILE *output, LONG width, LONG height, unsigned char *buffer)
{
    f8bppHeader_.bfSize = (DWORD)(s8bppHeader_ + width * height);

    i8bppHeader_.biWidth = (LONG)width;
    i8bppHeader_.biHeight = -(LONG)height;

    fwrite(&f8bppHeader_, sizeof(BITMAPFILEHEADER), 1, output);
    fwrite(&i8bppHeader_, sizeof(BITMAPINFOHEADER), 1, output); // I get the error in this line
    fwrite(p8bpp, sizeof(RGBQUAD), 256, output);
    fwrite(buffer, width, height, output);
}

不确定你需要这个,但我也会添加它。

void BmpHelper::Init8bppHeaders()
{
    f8bppHeader_.bfType = 'MB'; // Will be inverted during the fwrite
    s8bppHeader_ = (DWORD)(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD) + 256 * sizeof(RGBQUAD));
    f8bppHeader_.bfReserved1 = 0;
    f8bppHeader_.bfReserved2 = 0;
    f8bppHeader_.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);

    i8bppHeader_.biSize = sizeof(BITMAPINFOHEADER);
    i8bppHeader_.biPlanes = 1;
    i8bppHeader_.biBitCount = 8;
    i8bppHeader_.biCompression = BI_RGB;
    i8bppHeader_.biSizeImage = 0;
    i8bppHeader_.biXPelsPerMeter = 0;
    i8bppHeader_.biYPelsPerMeter = 0;
    i8bppHeader_.biClrUsed = 0;
    i8bppHeader_.biClrImportant = 0;

    for (size_t index = 0; index < 256; ++index)
    {
        p8bpp[index].rgbBlue = (BYTE)index;
        p8bpp[index].rgbGreen = (BYTE)index;
        p8bpp[index].rgbRed = (BYTE)index;
        p8bpp[index].rgbReserved = 0;
    }
}

如果我从路径中删除日期它可以正常工作,但它只保存带有给定的url +名称和文件格式的文件。我需要日期来区分文件。你们有没有发现代码有什么问题?提前谢谢。

1 个答案:

答案 0 :(得分:0)

您的日期/时间字符串包含:个字符。 Windows文件名不接受任何这些字符\/:*?"<>|。您必须使用其他字符替换:

此外,您的代码不考虑位图填充。仅当位图宽度(以字节为单位)为4的倍数时才有效。使用以下公式计算size(而不是width * height):

int size = ((width * bpp + 31) / 32) * 4 * height;

s8bppHeader_ = (DWORD)...是一个错字! bfSize计算不正确。

您可以使用C ++函数std::fstream而不是混合使用C ++和C

示例:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
#include <windows.h>

...
std::time_t t = std::time(nullptr);
char date[100];
std::strftime(date, sizeof(date), " %c", std::localtime(&t));
for(size_t i = 0, len = strlen(date); i < len; i++)
    if(date[i] == ':')
        date[i] = ','; //use a different character

char *Name = "_abc";
string url("c:\\test\\");
string name(Name);
string extension(".bmp");
string path = url + name + date + extension;

int width = i8bppHeader_.biWidth;
int height = i8bppHeader_.biHeight;
int bpp = i8bppHeader_.biBitCount;

int size = ((width * bpp + 31) / 32) * 4 * height;

f8bppHeader_.bfType = 'MB'; // Will be inverted during the fwrite
f8bppHeader_.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
    + 256 * sizeof(RGBQUAD) + size;
f8bppHeader_.bfReserved1 = 0;
f8bppHeader_.bfReserved2 = 0;
f8bppHeader_.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
    + 256 * sizeof(RGBQUAD);

ofstream fout(path, ios::binary);
fout.write((char*)&f8bppHeader_, sizeof(f8bppHeader_));
fout.write((char*)&i8bppHeader_, sizeof(i8bppHeader_));
fout.write((char*)p8bpp, 256 * sizeof(RGBQUAD));
fout.write((char*)buffer, size);