Home > Software design >  How to write correctly in file in C?
How to write correctly in file in C?

Time:05-02

I have this function:

void write_in_the_log_file(FILE *log_file, int tm_year, int tm_mon, int tm_mday,
                           int tm_hour, int tm_min, int tm_sec)
{
    log_file = fopen(ODIT, "a");
    fprintf(log_file, "Inventory at d.d.d - d:d:d\n", tm_mday, tm_mon, tm_year, tm_hour, tm_min,tm_sec);
    f_print_inventory(log_file);
    fprintf(log_file, "--------------------------------------------------------------\n");
    fclose(log_file);
}

And I call it like this:

  write_in_the_log_file(log_file, 
                        tm.tm_year   1900, 
                        tm.tm_mon   1, 
                        tm.tm_mday, 
                        tm.tm_hour, 
                        tm.tm_min, 
                        tm.tm_sec);

Where tm things are the time and they are declared like this:

    time_t T = time(NULL);
    struct tm tm = *localtime(&T);

Also ODIT is:

#define ODIT "odit/log_file.txt"

The problem here is that it is saving it correctly but after it the program crashes with error:

free(): double free detected in tcache 2
Aborted (core dumped)

CodePudding user response:

If you are using Mac or Linux, then this path location system can be used, but if you are using Windows OS, then Kindly change the path as per windows path system

try this

#include<stdlib.h>
#include <stdio.h>
#include <unistd.h>
void printData(void);
void ReadData(void);
void printData1(void);
void ReadData1(void);
void MergingFile(void);
int main(int argc, char const *argv[])
{
    printData();
    printData1();
    ReadData();
    ReadData1();
    MergingFile();
    return 0;
}
void printData()
{
    char String[1000]; FILE *file = NULL;
    printf("%s\n\n","Write Few Lines of '30 words' in files to test in File 1 ");
    scanf("%[^\n]",String);
    file = fopen("/Users/hemant/firstFile.txt","w");
    fprintf(file, "%s",String);
    fclose(file);
}
void printData1()
{
    char Stringn[1000];
    FILE *file2 = NULL;
    printf("%s\n\n","Write Few Lines of '30 words' in files to test 2 ");
    scanf("%[^\n]",Stringn);
    file2 = fopen("/Users/hemant/secondFile.txt","w");
    fprintf(file2, "%s",Stringn);
    fclose(file2);
}
void ReadData()
{
       char str[1000];
        FILE *file3 = NULL;
        printf("%s\n\n","You are Reading from First File ");
        file3 = fopen("/Users/hemant/firstFile.txt","r");
        fscanf(file3,"%[^\n]",str);
        printf("%s\n",str);
        fclose(file3);
}
void ReadData1()
{
       char str[1000];
        FILE *file4 = NULL;
        printf("%s\n\n","You are Reading from Second File ");
        file4 = fopen("/Users/hemant/secondFile.txt","r");
        fscanf(file4,"%[^\n]",str);
        printf("%s\n",str);
        fclose(file4);
}
void MergingFile()
{
    int i = 0, j = 0;
    char str[2000],String1[1000],String2[1000];
    FILE *fp = NULL;
    FILE *first = NULL;
    FILE *second = NULL;
    printf("%s\n\n","You are Merging Both firstFile and Second Data into ThirdFile ");
    fp = fopen("/Users/hemant/ThirdFile.txt", "w");
    first = fopen("/Users/hemant/firstFile.txt", "r");
    second = fopen("/Users/hemant/secondFile.txt", "r");
    fscanf(first,"%[^\n]",String1);
    fscanf(second, "%[^\n]",String2);
    while (String1[i] != '\0') {
           str[j] = String1[i];
           i  ;
           j  ;
       }
       i = 0;
       while (String2[i] != '\0') {
           str[j] = String2[i];
           i  ;
           j  ;
       }
       str  [j] = '\0';
    
    fprintf(fp,"%s",str);
    
    fclose(fp);
    fclose(first);
    fclose(second);
}

CodePudding user response:

There are at least 2 problems in your function:

  • what is the point of taking log_file as an argument in function write_in_the_log_file? If this argument is a valid stream pointer to the log file, use it directly instead of reopening the log file. Otherwise remove this argument and define log_file as a local variable.

  • you do not test if fopen() succeeded at opening the log file. ODIT is a relative path "odit/log_file.txt" so fopen() may fail to open the log file if the current directory is not what you assume. You should test the return value and avoid undefined behavior if log_file is a null pointer.

Without a compilable program that exhibits the offending behavior, it is impossible to ascertain where the double free error originates. It looks like a side effect of code with undefined behavior such as the write_in_the_log_file if log_file is null, but this undefined behavior could also be anywhere in the rest of the program.

Here is a modified version:

#include <errno.h>
#include <stdio.h>
#include <string.h>

void write_in_the_log_file(int tm_year, int tm_mon, int tm_mday,
                           int tm_hour, int tm_min, int tm_sec)
{
    FILE *log_file = fopen(ODIT, "a");
    if (log_file == NULL) {
        fprintf(stderr, "error opening log file %s: %s\n",
                ODIT, strerror(errno));
    } else {
        fprintf(log_file, "Inventory at d.d.d - d:d:d\n",
                tm_mday, tm_mon, tm_year, tm_hour, tm_min, tm_sec);
        f_print_inventory(log_file);
        fprintf(log_file, "--------------------------------------------------------------\n");
        fclose(log_file);
    }
}
  •  Tags:  
  • c
  • Related