1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
int usage(char* prog_name) {
printf("%s output_file_name length (in Megabytes)\n", prog_name);
return -1;
}
int main(int argc, char* argv[]) {
int rc = 0;
FILE* fp_outfile;
int len, count, wrote;
unsigned char bytes[1024 * 1024];
struct timeval start, finish, difference;
memset(bytes, 0xAA, sizeof(bytes));
if (argc < 3) {
return usage(argv[0]);
} else {
len = atoi(argv[2]);
count = len;
if (len > 0) {
fp_outfile = fopen(argv[1], "w+");
if (fp_outfile != NULL) {
gettimeofday(&start, NULL);
while (len > 0) {
wrote = fwrite(bytes, 1, sizeof(bytes), fp_outfile);
if (wrote != sizeof(bytes)) {
printf("error %d during fwrite\n", ferror(fp_outfile));
}
--len;
}
gettimeofday(&finish, NULL);
timersub(&finish, &start, &difference);
fclose(fp_outfile);
printf("wrote %d megabytes in %d seconds and %d microseconds\n",
count, (int)difference.tv_sec, (int)difference.tv_usec);
} else {
rc = errno;
printf("error %d opening file %s\n", rc, argv[1]);
}
} else {
printf("error %d length %d is not permitted\n", EINVAL, len);
rc = EINVAL;
}
}
return rc;
} |
Partager