Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6744727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:07:06+00:00 2026-05-26T12:07:06+00:00

Here is my current problem: I am trying to create a file of x

  • 0

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 file they want created. Later on in this project i’m gonna do other things with it but I’m stuck on the first step of creating the darn thing.

My problem code (so far):

        char empty[1024];
        for(int i = 0; i < 1024; i++)
        {
            empty[i] = 0;
        }

        fileSystem = fopen(argv[1], "w+");


        for(int i = 0; i < 1024*fileSize; i++){
            int temp = fputs(empty, fileSystem);
            if(temp > -1){
                //Sucess!
            }
            else{
                cout<<"error"<<endl;
            }
        }

Now if i’m doing my math correctly 1 char is 1byte. There are 1024 bytes in 1KB and 1024KB in a MB. So if I wanted a 2 MB file, i’d have to write 1024*1024*2 bytes to this file. Yes?

I don’t encounter any errors but I end up with an file of 0 bytes… I’m not sure what I’m doing wrong here so any help would be greatly appreciated!

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:07:06+00:00Added an answer on May 26, 2026 at 12:07 pm

    Potentially sparse file

    This creates output.img of size 300 MB:

    #include <fstream>
    
    int main()
    {
        std::ofstream ofs("ouput.img", std::ios::binary | std::ios::out);
        ofs.seekp((300<<20) - 1);
        ofs.write("", 1);
    }
    

    Note that technically, this will be a good way to trigger your filesystem’s support for sparse files.

    Dense file – filled with 0’s

    Functionally identical to the above, but filling the file with 0’s:

    #include <iostream>
    #include <fstream>
    #include <vector>
    
    int main()
    {
        std::vector<char> empty(1024, 0);
        std::ofstream ofs("ouput.img", std::ios::binary | std::ios::out);
    
        for(int i = 0; i < 1024*300; i++)
        {
            if (!ofs.write(&empty[0], empty.size()))
            {
                std::cerr << "problem writing to file" << std::endl;
                return 255;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my current question: I'm guessing that my problem (described below) is being
Here is my current setup: Database Role - MyDbRole Schema - MySchema User -
I am trying to create a car make/model form using Javascript or AJAX, problem
I'm trying to create a Regex javascript split, but I'm totally stuck. Here's my
I am basically trying to create a display object transformation manager which will allow
I'm trying to improve my current algorithm for the 8 Queens problem, and this
How do I do this properly. I'm trying to name the sql file that
In my Rails app, if the user is trying to create a new account,
I'm trying to build a search engine that will check a list and then
I'm trying to figure out the correct approach (and pattern) that my current problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.