C++ Make a file of a specific size

前端 未结 3 1640
刺人心
刺人心 2021-01-02 09:03

Here is my current problem: I am trying to create a file of x MB in C++. The user will enter in the file name then enter in a number between 5 and 10 for the size of the fil

3条回答
  •  没有蜡笔的小新
    2021-01-02 09:12

    Your code doesn't work because you are using fputs which writes a null-terminated string into the output buffer. But you are trying to write all nulls, so it stops right when it looks at the first byte of your string and ends up writing nothing.

    Now, to create a file of a specific size, all you need to do is to call truncate function (or _chsiz for Windows) exactly once and set what size you want the file to be.

    Good luck!

提交回复
热议问题