开发者

Problem with writing out large temp files

I have a set of large image files that I'm using as temporary swap files on Windows in Visual Studio 2010. I'm writing and reading the files out as necessary.

Problem is, even though each of the files are the same size, I'm getting different file sizes.

So, I can do:

template <typename T>
std::string PlaceFileOnDisk(T* inImage, const int& inSize)
    TCHAR lpTempPathBuffer[MAX_PATH];
    TCHAR szTempFileName[MAX_PATH];
    DWORD dwRetVal = GetTempPath(MAX_PATH, lpTempPathBuffer);
    UINT uRetVal = GetTempFileName(lpTempPathBuffer, TEXT("IMAGE"), 0, szTempFileName);

    FILE* fp;
    fopen_s(&fp, szTempFileName, "w+");
    fwrite(inImage, sizeof(T), inSize, fp);
    fclose(fp);

    std::string theRealTempFileName(szTempFileName);

    return theRealTempFileName;

}

but that results in files between 53 and 65 mb in size (the image is 4713 * 5908 * sizeof (unsigned short).

I figured that maybe that 'fwrite' might not be stable for large files, so I broke things up into:

template <typename T>
std::string PlaceFileOnDisk(T开发者_StackOverflow中文版* inImage, const int& inYSize, const int& inXSize)
    TCHAR lpTempPathBuffer[MAX_PATH];
    TCHAR szTempFileName[MAX_PATH];
    DWORD dwRetVal = GetTempPath(MAX_PATH, lpTempPathBuffer);
    UINT uRetVal = GetTempFileName(lpTempPathBuffer, TEXT("IMAGE"), 0, szTempFileName);

    int y;
    FILE* fp;
    for (y = 0; y < inYSize; y++){
        fopen_s(&fp, szTempFileName, "a");
        fwrite(&(inImage[y*inXSize]), sizeof(T), inXSize, fp);
        fclose(fp);
    }

    std::string theRealTempFileName(szTempFileName);

    return theRealTempFileName;
}

Same thing: the files that are saved to disk are variable sized, not the expected size.

What's going on? Why are they not the same?

The read in function:

template <typename T>
T* RecoverFileFromDisk(const std::string& inFileName, const int& inSize){

    T* theBuffer = NULL;
    FILE* fp;
    try {
        theBuffer = new T[inYSize*inXSize];
        fopen_s(&fp, inFileName.c_str(), "r");
        fread(theBuffer, sizeof(T), inSize, fp);
        fclose(fp);
    }
    catch(...){
        if (theBuffer != NULL){
            delete [] theBuffer;
            theBuffer = NULL;
        }
    }
    return theBuffer;
}

This function may be suffering from similar problems, but I'm not getting that far, because I can't get past the writing function.

I did try to use the read/write information on this page: http://msdn.microsoft.com/en-us/library/aa363875%28v=vs.85%29.aspx

But the suggestions there just didn't work at all, so I went with the file functions with which I'm more familiar. That's where I got the temp file naming conventions, though.


Are you able to open the image after it's written? It sounds like you're having trouble writing it, too?

You're just asking about why the file sizes are different for the same size picture? What about how the sizes of the initial files compare to each other? It may have something to do with the how the initial image files are compressed.

I'm not sure what you're doing with the files, but have you considered a more basic "copy" function?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜