在C中清除文本文件 [英] Text file being cleared in C

查看:62
本文介绍了在C中清除文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Lunix C程序,它将配置参数存储在文本文件中。

我从文本文件中读取

I have a Lunix 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

FILE *file = fopen(filename, "w"):



I我遇到问题,文本文件被清除,下次我来读它时它是空白的。

我明白当文件打开写入时会被覆盖,我的程序在读完文件后会存储文件的内容并将其写回。

它在主要文件中写得正确,但偶尔我会发现文本文件是空白的。



我最初的想法是这可能是因为程序被无情地停止了,在写入文件的过程中,将其留空或者有2个程序运行的实例,并且打开它进行写入,另一个读取它,这意味着它将读入空白文件,然后在写出时用空白文件覆盖它。

经过一些测试后,似乎并非如此。



这使我不确定是什么原因导致文本文件被清除。

有没有人有任何想法?

或者之前有人见过这样的东西吗?



谢谢,任何帮助将不胜感激。




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.

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 */

推荐答案

在fopenw表示 - 创建一个空文件并打开它进行写作!

如果你想保留以前的内容,请使用a而不是w ...
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"...


我想你把变量(number_of_lines)保存为全局变量。您可能想要观看此变量。如果它被清除,你将写一个空文件。

我建议你在用w进行fopen之前检查这个变量。
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".


这篇关于在C中清除文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆