English 中文(简体)
如何将文件大小设置为 C++ 中的 Windows 100 字节?
原标题:How to set file size to 100 Bytes in Windows in C++?

请告诉您如何将文件最大大小设置为 100 位元。 如果我试图写入超过 100 位元的更多数据, 则 < code> untwritefile () 应当丢弃错误 < code> ERROR_ NOT_ ENOUGH_ MEMORY

请建议我, 一个设置 100 字节( 或固定大小) 文件的 Widnows API 。

在我们的产品中, ntwritefile () 错误失败 。 我试图理解, 在什么情况下, 我们会得到错误 。

问题回答

使用 SetFilePointer ,然后是 SetEndofFile 。在 SetEndofFile 之后,不要忘记 CloseHandle !

在 Windows 上,这样看起来会是这样:

  1. CreateFile
  2. WriteFile

就是这样。

您可以创建大小为 100 字节的缓冲符, 完成后将缓冲符写入文件 。

#include <stdio.h>
#include <memory.h>


int main(int argc, char* argv[])
{
    char buffer[100];

    //assign values
    memset(buffer,  A , 100);
    buffer[5]= @ ;

    FILE * pFile;
    pFile = fopen ( "buffer.100" , "wb" );
    fwrite (buffer, 1, sizeof(buffer) , pFile );
    fclose (pFile);

    return 0;
}

您要创建100字节的文件吗? 还是要将文件大小限制为100字节? 意味着如果文件超过100字节, 文件上的任何进一步写作都不会有效?

如果在那之后您第一次重写它, 您可以使用 CreateFile writeFile , 并将缓冲大小限制在100字节。

如果它的第二个... 那么我想它不可能...

从建筑学角度看,你的最佳方法如下:

  1. Identify how you re reading from and writing to the file in question.
  2. Create an inteface that provides these same facilities via a single managed class. The API would be as near as possible the same to what you already have.
  3. Add code, encapsulated in your manager class to track the size of the buffer as you write, probably also buffering the 100 bytes that you want in your file. Code can then detect and handle overflow situations in whatever way you deceide (i.e. throwing exceptions if that s what you want.)

I m sorry you don t need a buffer for this. Simply if you create a memory map object of file ,which is greater than the file size, the system automatically expand the file into that size.

But for just 100bytes you just create a 100 bytes of dummy char array and write it to the file.

see, http://msdn.microsoft.com/en-us/library/windows/desktop/aa366542(v=vs.85).aspx





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签