I am currently working on the following issues in C language, but could someone please tell me how to correct the output to the log file in a different format from the example, and the fact that only one record can be saved and printed?
Save the results and date, time, and seconds of the operation as
The log limit should be 1000 lines (no consideration should be given to cases where the limit is exceeded)
Example) Record the following in log.txt
April 27, 2015 14:30:51, 5+6,11
Current State Output:
Fri Nov 11 10:51:27 2022
5+510.000000
Current State Code:
#include<stdio.h>
# include <time.h>
int main() {
int num1, num2;
charop;
float answer;
intr;
FILE*fp;
fp = fopen("log.txt", "w");
if(fp==NULL){
printf("File Open Failed\n";
return-1;
}
r = scanf("%d%c%d", & num1, & op, & num2);
if(r!=3){
puts("input error");
return1;
}
if(op=='+'){
answer = num1 + num2;
}
else if(op=='-'){
answer = num1-num2;
}
else if(op=='*'){
answer = num1 * num2;
}
else if(op=='/'){
answer=(float) num1/num2;
}
printf("%f\n", answer);
time_tt = time(NULL);
char*s = ctime(&t);
printf("%s",s);
fprintf(fp, "%s%d%c%d%f\n", s, num1, op, num2, answer);
fclose(fp);
return 0;
}
Example) The output in a different format from is a problem with the method you chose to string the date and time information. You can use this article to convert the value of type The fact that only one record can be saved and output is related to what @774RR commented on, and if you want to do so many calculations while continuing to run the program you created, you must loop it repeatedly while opening the log file as shown in the comment. Otherwise, if the program itself performs input, operation, and log output only once, and repeats the program execution many times, the following causes and actions will be taken: This is because the file opening mode is written (newly created) in this line. If you open it in
The ctime()
function is not in the format shown in Example ).
time_t
to the tm
structure and specify the data of the year, time, minute, and second individually in the fprintf()
function, or the format you want to output in the strftime()
function.
Structure in the string fp=fopen("log.txt", "w");
append
mode like this, it should be added.fp=fopen("log.txt", "a");
© 2024 OneMinuteCode. All rights reserved.