• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

help me write a c++ function!

gplanet

Senior member
I need to write a function in C++ (for leisure not school) that does the following:

The function creates a file and names the file using a string (like string = "uselessfile.txt")
The file is given a random byte size that can be any reasonable size so it's not 0 bytes.

so i would have to put that information into this file somehow. Also, it would need to be very efficient. Lets say i wanted the file to be 5MB, it would be a bad idea if it froze up the program for 30 seconds.

i have no idea how to do any of this

Any help?
 
Ill show you how in C# it should give you a start to eiether write it in c or move to c# 🙂


<FONT color=#808080 size=2>
///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2><summary>
</FONT><FONT color=#808080 size=2>///</FONT><FONT color=#008000 size=2> This function creates a garbage file
</FONT><FONT color=#808080 size=2>///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2></summary>
</FONT><FONT color=#808080 size=2>///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2><param name="size"></FONT><FONT color=#008000 size=2>size of the file to be created</FONT><FONT color=#808080 size=2></param>
</FONT><FONT color=#808080 size=2>///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2><param name="filename"></FONT><FONT color=#008000 size=2>the name of the file to be created</FONT><FONT color=#808080 size=2></param>
</FONT><FONT color=#808080 size=2>///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2><returns></FONT><FONT color=#008000 size=2>if the file creation worked</FONT><FONT color=#808080 size=2></returns></FONT><FONT size=2>
</FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>bool</FONT><FONT size=2> createFile(</FONT><FONT color=#0000ff size=2>uint</FONT><FONT size=2> size, </FONT><FONT color=#0000ff size=2>string</FONT><FONT size=2> filename)
{
</FONT><FONT color=#0000ff size=2>
try
</FONT><FONT size=2>{
Byte[] random = </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> Byte[100000];
Random randgen = </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> Random();
Stream outputStream = File.OpenWrite(@filename);
</FONT><FONT color=#0000ff size=2>if</FONT><FONT size=2>(outputStream == </FONT><FONT color=#0000ff size=2>null</FONT><FONT size=2>)
</FONT><FONT color=#0000ff size=2>return</FONT><FONT size=2>(</FONT><FONT color=#0000ff size=2>false</FONT><FONT size=2>);
</FONT><FONT color=#0000ff size=2>uint</FONT><FONT size=2> loop = size / 100000;
</FONT><FONT color=#0000ff size=2>uint</FONT><FONT size=2> modulo = size % 100000;
Byte[] extra = </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> Byte[modulo];
randgen.NextBytes(extra);
</FONT><FONT color=#0000ff size=2>for</FONT><FONT size=2>(</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> i = 0; i < loop; i++)
{
randgen.NextBytes(random);
outputStream.Write(random,0,random.Length);
}
outputStream.Write(extra,0,extra.Length);
outputStream.Close();
</FONT><FONT color=#0000ff size=2>return</FONT><FONT size=2>(</FONT><FONT color=#0000ff size=2>true</FONT><FONT size=2>);
}
</FONT><FONT color=#0000ff size=2>catch</FONT><FONT size=2>(Exception e)
{
</FONT><FONT color=#0000ff size=2>if</FONT><FONT size=2>(e != </FONT><FONT color=#0000ff size=2>null</FONT><FONT size=2>)
</FONT><FONT color=#0000ff size=2>return</FONT><FONT size=2>(</FONT><FONT color=#0000ff size=2>false</FONT><FONT size=2>);
}
</FONT><FONT color=#0000ff size=2>return</FONT><FONT size=2>(</FONT><FONT color=#0000ff size=2>false</FONT><FONT size=2>);
}






the class you would use in c++ is ofstream

have fun</FONT>
 
try this:

/// <summary>
/// This function creates a garbage file to send out over an SA
/// </summary>
/// <param name="size">size of the file to be created</param>
/// <param name="filename">the name of the file to be created</param>
/// <returns>if the file creation worked</returns>
public bool createFile(uint size, string filename)
{
try
{
Byte[] random = new Byte[globals.BLOCKSIZE];
Random randgen = new Random();
Stream outputStream = File.OpenWrite(@filename);
if(outputStream == null)
return(false);
uint loop = size / globals.BLOCKSIZE;
uint modulo = size % globals.BLOCKSIZE;
Byte[] extra = new Byte[modulo];
randgen.NextBytes(extra);
for(int i = 0; i < loop; i++)
{
randgen.NextBytes(random);
outputStream.Write(random,0,random.Length);
}
outputStream.Write(extra,0,extra.Length);
outputStream.Close();
return(true);
}
catch(Exception e)
{
if(e != null)
return(false);
}
return(false);
}
 
This is what i came up with on my own. But the files are created really slow, like 100kb/s. Maybe this is some kind of DOS limitation but i dunno windows programming.

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

string getName(const string prompt);
int numTracks();
void createFile(string name, int bytes);

void main() {
char add = 'y';
string artist, album, temp;
vector<string>title;

do {
title.resize(numTracks());

cin.ignore();
artist = getName("Enter artist: ");
album = getName("Enter album: ");

for(int i = 0; i < title.size(); i++)
title = artist + " - " + album + " - " + getName("Enter track: ") + ".mp3";
for(i = 0; i < title.size(); i++)
createFile(title, (rand() % 15000 + 15000)/20);

cout << endl << "Another album? (y/n): ";
cin >> add;

}while(add=='y');
}

string getName(const string prompt) {
char a[30];

cout << endl << prompt;
cin.getline(a, sizeof(a));

return a;
}

int numTracks() {
int n;

cout << "Enter the number of tracks: ";
cin >> n;

return n;
}

void createFile(string name, int bytes) {
double a = 1234567890123456789.123456789012345678901234567890123456789;

ofstream outfile(name.c_str());

for(int i = 0; i < bytes; i++)
outfile << a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a
<< a << a << a << a << a << a << a << a << a << a;
}

(i tried outputting a lot of the a's to run less durations of the loop but it doesn't help much)
 
can you show me what you would change in my code?


ameesh, it's part of a project i'm working on. and i don't know what's wrong with fake mp3 files
 


<<
and i don't know what's wrong with fake mp3 files
>>



It's possibly disingenuous and possibly mean-spirited. That'd be no good.

-geoff
 


<<

<<
and i don't know what's wrong with fake mp3 files
>>



It's possibly disingenuous and possibly mean-spirited. That'd be no good.

-geoff
>>



possibly? id say very likely.
 
Whoa, what horrible functions you guys write 🙂

Here's mine in C(++) that's very fast:


#include <stdio.h>
#include <stdlib.h>

void WriteFile(const char *pstrFileName, int kb_multiplier)
{
FILE *pFile = fopen(pstrFileName, "wb");
char buffer[1024];

if(!pFile)
return;

for(int i=0; i < kb_multiplier; i++)
{
fwrite((void*)buffer, sizeof(buffer), 1,
pFile);
}

fclose(pFile);
}

void main(void)
{
WriteFile("c:\\garb.sx", 50000);
}
 


<< Whoa, what horrible functions you guys write 🙂

Here's mine in C(++) that's very fast:


#include <stdio.h>
#include <stdlib.h>

void WriteFile(const char *pstrFileName, int kb_multiplier)
{
FILE *pFile = fopen(pstrFileName, "wb");
char buffer[1024];

if(!pFile)
return;

for(int i=0; i < kb_multiplier; i++)
{
fwrite((void*)buffer, sizeof(buffer), 1,
pFile);
}

fclose(pFile);
}

void main(void)
{
WriteFile("c:\\garb.sx", 50000);
}
>>




the file created will probably compress really well since you dont intialize the array it will write the same set of zeros down to disk over and over again. ususlaly you dont want the garbage files to compress, they should be fulll of random data.
 
the file created will probably compress really well since you dont intialize the array it will write the same set of zeros down to disk over and over again. ususlaly you dont want the garbage files to compress, they should be fulll of random data.

Ha! C does not initialize your memory upon allocation. I wish it would...
It won't be random data, but it will be whatever was in it before - for example text from notepad or something less beautiful.
 
"Randomizing" takes time 🙂 I don't think gplanet said anything about compression. Although, it would be interesting to see who could write the shortest program generating the most random data in the least amount of time 🙂
 
Back
Top