logFile = fopen(logfilename, "a"); getting an error of error: no match for ‘operator=’…
- User
logFile = fopen(log_file_name, "a"); getting an error of error: no match for ‘operator=’ (operand types are ‘FILE’ {aka ‘_IO_FILE’} and ‘FILE*’ {aka ‘_IO_FILE*’}) 126 | logFile = fopen(log_file_name, "a");
- Assistant
The error you are encountering is due to trying to assign a FILE pointer to a FILE object. The fopen function returns a FILE pointer, so you should declare logFile as a FILE pointer instead of a FILE object.
You can fix this error by changing the declaration of logFile to a FILE pointer like this:
FILE *logFile; logFile = fopen(log_file_name, "a");This way, you are assigning a FILE pointer returned by the fopen function to the logFile pointer variable.
- User
write a c function to check whether there is a log folder is there and create a log.txt file if not exist and pass the file name to that function and create another function to append KPI values in that opened file
- Assistant
Here is a sample C program that includes two functions: one to check if a "log" folder exists and create a "log.txt" file inside the folder if it does not exist, and another function to append KPI values to the opened file.
#include <stdio.h> #include <stdlib.h> #include <sys/stat.h> // Function to check if log folder exists and create log.txt file if it doesn't exist void createLogFile(const char *foldername, const char *filename) { char path[100]; snprintf(path, sizeof(path), "%s/%s", foldername, filename); // Check if log folder exists, if not then create it struct stat st = {0}; if (stat(foldername, &st) == -1) { mkdir(foldername, 0700); } // Create log.txt file inside log folder FILE *file = fopen(path, "a"); if (file == NULL) { printf("Error opening file!\n"); exit(1); } fclose(file); } // Function to append KPI values to the opened file void appendKPIValues(const char *filename, double kpiValue) { FILE *file = fopen(filename, "a"); if (file == NULL) { printf("Error opening file!\n"); exit(1); } fprintf(file, "%f\n", kpiValue); fclose(file); } int main() { const char *log_folder = "log"; const char *log_filename = "log.txt"; double kpi_value = 12.345; // Create log.txt file if not exist createLogFile(log_folder, log_filename); // Append KPI values to the opened file appendKPIValues(log_filename, kpi_value); return 0; } - User
not open that file continiously.. just open once then append
- Assistant
If you only want to open the file once and then append KPI values to it without opening and closing the file every time you want to append a value, you can modify the functions as follows:
#include <stdio.h> #include <stdlib.h> #include <sys/stat.h> FILE *logFile = NULL; // Function to check if log folder exists and create log.txt file if it doesn't exist void createLogFile(const char * foldername, const char * filename) { char path[100]; snprintf(path, sizeof(path), "%s/%s", foldername, filename); // Check if log folder exists, if not then create it struct stat st = {0}; if (stat(foldername, &st) == -1) { mkdir(foldername, 0700); } // Create log.txt file inside log folder logFile = fopen(path, "a"); if (logFile == NULL) { printf("Error opening file!\n"); exit(1); } } // Function to append KPI values to the opened file void appendKPIValues(double kpiValue) { if (logFile == NULL) { printf("Log file is not opened!\n"); return; } fprintf(logFile, "%f\n", kpiValue); } int main() { const char *log_folder = "log"; const char *log_filename = "log.txt"; double kpi_value = 12.345; // Create log.txt file if not exist createLogFile(log_folder, log_filename); // Append KPI values to the opened file appendKPIValues(kpi_value); // Close the file when done fclose(logFile); return 0; } - User
function to close the file
- Assistant
You can create a function to close the file using
fclose(logFile). Here's how you can modify the existing code to add a new functioncloseLogFileto close the file:#include <stdio.h> #include <stdlib.h> #include <sys/stat.h> FILE *logFile = NULL; // Function to check if log folder exists and create log.txt file if it doesn't exist void createLogFile(const char * foldername, const char * filename) { char path[100]; snprintf(path, sizeof(path), "%s/%s", foldername, filename); // Check if log folder exists, if not then create it struct stat st = {0}; if (stat(foldername, &st) == -1) { mkdir(foldername, 0700); } // Create log.txt file inside log folder logFile = fopen(path, "a"); if (logFile == NULL) { printf("Error opening file!\n"); exit(1); } } // Function to append KPI values to the opened file void appendKPIValues(double kpiValue) { if (logFile == NULL) { printf("Log file is not opened!\n"); return; } fprintf(logFile, "%f\n", kpiValue); } // Function to close the file void closeLogFile() { if (logFile != NULL) { fclose(logFile); logFile = NULL; // Reset the file pointer after closing } } int main() { const char *log_folder = "log"; const char *log_filename = "log.txt"; double kpi_value = 12.345; // Create log.txt file if not exist createLogFile(log_folder, log_filename); // Append KPI values to the opened file appendKPIValues(kpi_value); // Close the file when done closeLogFile(); return 0; }