I have a Linux C program that store configuration parameters in a text file. I read from the text file using
FILE *file = fopen(filename, "r"):
and write to the file using following code
FILE *file = fopen(filename, "w"):
I am getting a problem where the text file is cleared, it is blank the next time I come to read it. I understand that when the file is opened for write it gets overwritten, my program stores the contents of the file after it has read it in and writes it back out. It does in the main write it out correctly, but occasionally I will find that the text file is blank.
My initial thoughts were that this may be because the program was unsafely stopped, mid way through writing to the file leaving it blank or there were 2 instances of the program running and as one open it for writing, the other reads it in, meaning it would read in a blank file and then overwrite it with a blank file when it writes it out. After some testing this does not seem to be the case.
This leaves me unsure as to what is causing the text file to be cleared. Does anyone have any ideas?
See code below
char text_lines[200][54]; /* global variable */
void read_in_text_file()
{
/* this sub reads in the text file */
//printf("read in file\n");
/* declares the variables */
char line[128];
int counter = 0;
int length;
/* open a text file and read it in line by line, storing each line in a variable. also returning a value for the number of lines in each section */
static const char filename[] = "config.txt";
FILE *file = fopen(filename,"r"); /* opens the config file */
if (file==NULL){ /* checks if the file has successfully opened */
perror ("Error opening file"); /* displays error message on stderr - that returns reason file did not open */
printf("Error opening file\n"); /* tells the user that the file has not opened */
exit(0); /* exits the program if the text file can not be read in */
}
else{
//printf("the file is open\n");
while ( fgets ( line, sizeof line, file ) != NULL) /* reads each line of the text file */
{
sprintf(text_lines[counter],"%s",line); /* puts the line into a variable */
length = zstrlen(text_lines[counter]); /* calculates the length of the text not including \r or \n characters */
if(text_lines[counter][length-1] == '\n') /* checks if the last character is \n (a new line character) */
{
text_lines[counter][length-1] = '\0'; /* removes this new line character and replaces it with end of line identifier */
}
counter = counter + 1; /* uses a counter for each line */
} /* end of while loop */
number_of_lines = counter; /* puts the number of lines into a integer variable */
fclose(file); /* closes the file */
}
} /* end of sub for reading in the text file */
/* some changes may be made to the config before it is printed to the file again */
void print_to_text_file()
{
pthread_mutex_lock(&lock); /* block until thread has ownership */
/* sub for printing all the lines in the text_lines variable to the text file "config.txt" */
int counter;
static const char filename[] = "config.txt";
FILE *file = fopen(filename,"w"); /* opens the config.txt file, with write privileges */
if (file==NULL){ /* checks if the file has successfully opened */
perror ("Error opening file"); /* displays error message on stderr - that returns reason file did not open */
printf("Error opening file\n"); /* tells the user that the file has not opened */
}
else{
//printf("the file is open\n"); /* prints to the terminal screen the file has opened */
for (counter = 0; counter < number_of_lines; counter++) /* uses a for loop to scroll through all text lines */
{
// printf("%s\n",text_lines[counter]);
fprintf(file, "%s\n",text_lines[counter]); /* prints current text line to the file */
}
fclose(file); /* closes the file */
}
pthread_mutex_unlock(&lock); /* release blocking on thread */
} /* end of print text to file sub */