Hi everyone,
What I am trying to do is to read in a file, line by line, and write to a new file until the EOF. I also want to find out the time it takes for the program to run, one for CPU and one for real world time. My code is kind of a mess right now so I can use some help. Much appreciated.
Code:
#include <stdio.h>
#include <time.h>
#include <string.h>
int main(void)
{
clock_t begin, finish;
double time_spent;
time_t start, end;
double duration = 0;
begin = clock();
time(&start);
FILE *fpr, *fpw;
char file_name_r[]="";
char file_name_w[]="";
char string[BUFSIZ];
printf( "\nEnter the name of the file to write to: " );
scanf("%s", &file_name_w);
printf( "Enter the name of the file to open: " );
scanf("%s", &file_name_r);
fpr = fopen(file_name_r, "r");
if(fpr == NULL)
{
printf( "\n%s\ File NOT FOUND!",file_name_r);
}
while (fgets(string, sizeof string, fpr) != NULL){
fpw = fopen(file_name_w, "w");
fprintf(fpw, "%s", string);
}
fclose(fpr);
fclose(fpw);
return 0;
end = clock();
time_spent = (double)(finish - begin) / CLOCKS_PER_SEC;
printf("Time elapsed in CPU milli-seconds : %f milli-seconds/n", time_spent);
time(&end);
duration = difftime(end, start);
duration = duration/1000;
printf("Time elapsed in Wall-clock : %f milli-seconds\n", duration);
}