C++ file size and time diff
Most of my coding has been in java but thought of writing the test apps in C++. After all I am a student and C++ is my first love (okay its you baby :)).
Ove thing that I cud eaily do in Java was to get the file size. FILE.length function. But in C++ there was no way except seeking to the end of the function. So here is the code for it:
-----
static long getFileSize(FILE *stream)
{
long curpos = 0L;
long length = 0L;
/* Save current position */
curpos = ftell(stream);
/* Point to end of stream, offset from beginning */
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
/* Restore previous position */
fseek(stream, curpos, SEEK_SET);
return length;
}
To call:
FILE *file1 ;
file1= fopen(argv[1],"r");
inputSize += (double)((double) fileUtil::getFileSize(file1))/1024;// in KB
Also wrote a code get the time in real good granularity (nanosecs)
struct timespec t1,t2;
clock_gettime(CLOCK_REALTIME,&t1);
// CODE TO BE PROFILED
..............
clock_gettime(CLOCK_REALTIME,&t2);
double msec=(double)(t2.tv_sec - t1.tv_sec) * 1000 + (double)((double)(t2.tv_nsec - t1.tv_nsec) * (double)(0.000001));
Note: For this include time.h and compile with the library -lrt
0 Comments:
Post a Comment
<< Home