Over the last few months on and off i've been in contact with a friend through email and have made a little C++ program that I still don't quite grasp, (this is the first time i've done this language) What I have so far is a program that does let you create multiple files of a specified size, file 0,1,2 etc. What I need is someway of using a timer to state the amount of time this operation took after the file is made. It would be brilliant if there was someway of stating it in milliseconds, but any accurate timer will do.
e.g file 0 is made it took 1 second to make this file
Any help or suggestions is appreciated. I messed around with simple timers a month ago but could not get an accurate time displayed and got more and more confused :-{ Everytime file creation looped the time increased dramatically and it was an obvious error
#include <ctime> // For time()
#include <cstdlib> // For srand() and rand()
using namespace std;
//ofstream::pos_type size;
long size_kbs; //This will be the size of the file in kilobytes
char *some_data; //A pointer of type char
int max_files;
char filename[11];
char filenum[5];
int main (int argc, char* argv[]) {
if (argc < 3 ){ //argc is the number of command line arguments. If less than 2, file size was not specified
size_kbs = 32;
max_files = 1; //Default one file if number not specified
cout <<"File size was not specified so default 32KB will be used!" <<endl;
}
else {
size_kbs = atol(argv[1]); //If the file size was specified on command line then set size_kbs to it. argv[1] is the first argument after program name.
max_files = atoi(argv[2]);
cout <<"File size will be " <<size_kbs <<" kilobytes." <<endl <<"Number of files will be " <<max_files <<"." <<endl;
}
some_data = new char[1024]; //Dynamically allocate array to be 1KB in size.
srand(time(0)); // Initialize random number generator.
//Generate a pseudo-random 8 bit number between 1 and ten
//Do this 1024 times and fill up the array some_data
for (int i=0; i<1024; i++) {
char random = (char)((rand() % 10) + 1);
some_data[i] = random;
}
for(int i=0; i<max_files; i++) {//open for loop
sprintf(filenum, "%d", i); //Had to use this instead of itoa as it turns out itoa is not standard
strcpy(filename, "file"); //strcpy can be dangerous as it there is no check to make sure the area copied to is big enough for data.
strcat(filename, filenum);//same goes for strcat.
ofstream file (filename, ios::out|ios::binary); //Open a file caled "file.bin"
if (file.is_open()) //If file open succeeds
{
file.seekp (0, ios::beg); //Go to beginning of file
//Next we put the required amount of data in the file. //some_data is 1024 bytes or 1 kilobyte in size
//Repeat size_kbs times e.g 8 times for a 8KB file
for (int i=0; i<size_kbs; i++) {
file.write (some_data, 1024);
}
cout <<filename <<" written." <<endl; //Notify that file is written
file.close(); //Close the file now we are finished with it.
}
else cout << "Unable to open file"; //If file open failed
}
delete[] some_data; //Release the memory used by some_data
I've looked this up, and helios is right, it is a unix only call.
time.h in windows is only accurate to seconds, if you want milliseconds you'll have to use a library, or somewhat fudge it with ticks.
Have a look at GetFrequencyCounter() (I think it's called). There are a set of Frequency functions that offer a much higher resolution of accuracy. The ones used in gaming for getting the time between frames when rendering :)