Click here to Skip to main content
15,896,493 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Lunix C program that store configuration parameters in a text file.
I read from the text file using
C#
FILE *file = fopen(filename, "r"):

and write to the file using
C#
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 unsafly 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?
or has anyone seen something like this before?

Thanks, any help will be appreciated.

Objective-C
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 */
Posted
Updated 26-Jan-14 23:18pm
v3
Comments
José Amílcar Casimiro 23-Jan-14 12:19pm    
Use "Improve question" option and past some code.
Richard MacCutchan 23-Jan-14 12:39pm    
I would suggest that there is some circumstance where you open the file for writing but do not actually write anything into it. Show some of the code, or use your debugger to try and find out why.
idenizeni 23-Jan-14 14:07pm    
Should this be tagged as C# or C?
CPallini 23-Jan-14 15:59pm    
If your program is 'unsafely stopped' then the pending output (OS uses buffers) is not transferred to the disk.
Member 10190163 30-Jan-14 8:46am    
I have now shown more code.

In fopen "w" means - create me an empty file and open it for writing!
If you want to keep the previous content use "a" instead "w"...
 
Share this answer
 
Comments
CPallini 23-Jan-14 13:17pm    
5.
Matt T Heffron 23-Jan-14 14:36pm    
OP indicates this is known and the whole file contents are being rewritten.
Kornfeld Eliyahu Peter 23-Jan-14 14:41pm    
Yes but IMHO (based on my experience) he has a wrong flow - so replacing "w" with "a" will give him the opportunity to see and solve the real problem...
Matt T Heffron 23-Jan-14 14:57pm    
If he needs to CHANGE part of the config file contents (e.g., a default file path) he WILL need to REWRITE the file, append will be wrong.
Kornfeld Eliyahu Peter 23-Jan-14 15:01pm    
For sure I do not know what he exactly one - there is no enough information for that, however using append probably will give him a wrong content, but also will reveal his flow - and that will point out to him the problem with his program...
I guess you keep the variable (number_of_lines) as global variable. You might want to watch this variable. If it get cleared, you will write an empty file.
I will recommend you to check this variable before you do a fopen with "w".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900