自動創建同類不同名的文件函數(C語言)

前言
  1. 工作太忙了,先不寫介紹了,功能都在程序介紹裏;
源程序

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>

int is_in(char *s, char *c);
char filename[50];

int creat_now_file( char * path,char * prefix, char * file_path);

int main(int argc, char *argv[])
{

	char file_path[50];
	if( creat_now_file("/online/miss_log/","main",file_path) == 0 )
	{
		printf("文件%s創建成功!\n",file_path);
	} 
	else
	{
		printf("文件創建失敗!!!\n");
	}

	return 0;
}


/*
函數名:creat_now_file
功能:在file_path_profix指定的目錄下添加file_name_prefix開頭的不重名文件。
			  同時,如果同前綴文件數量超過了10個,函數會刪除最早的一個文件名;
參數:
	file_path_prefix(int) : 指定文件的路徑,不包含文件名和索引名;
	file_name_prefix(int) : 指定文件索引名,不包含路徑名和路徑索引;
	file_path(out) : 輸出新建的文件名的完整路徑;
返回值:
	  0:文件創建成功;
	-1:file_path_prefix所指定的路徑打開失敗;
	-2:新文件創建失敗;
	-3:最小文件刪除失敗;
*/

int creat_now_file( char * file_path_profix,char * file_name_prefix, char * file_path)
{
	unsigned int max = 0,min = 0,equal_file_name_count = 0,i = 0;

	static char path[100];
	char path_name_temp[100];
	DIR *directory_pointer;
	struct dirent *entry;

	/********************遍歷文件內的文件*********************/
	if((directory_pointer=opendir(file_path_profix))==NULL)
	{
		printf( "%s Error opening \n",file_path_profix );
		return -1;
	}
	else
	{
		unsigned  int postfix_value;
		
		while((entry=readdir(directory_pointer))!=NULL)
		{
			memset( path_name_temp,'\0',sizeof( path_name_temp ) );
			strcat(path_name_temp,file_name_prefix);
			strcat(path_name_temp,"%u");
			//printf("path_name_temp = %s\n",path_name_temp);
			//printf("%s\n",entry-> d_name);//輸出當前文件夾下所有的文件名;
			if(is_in(entry->d_name,file_name_prefix)==1)
			{
				printf("%s\n",entry-> d_name);	
				//篩選出最大值和最小值文件名後綴;
				if( sscanf(entry->d_name,path_name_temp,&postfix_value) == 1 ) 
				{
					if( i == 0) 
					{
						min = postfix_value;
						max = postfix_value;
						// printf("0文件名後綴最大值和最小值:max == %u	min == %u\n",max,min);
						i = 1;
					}
					
					if( postfix_value <= min ) min = postfix_value;
					if( postfix_value >= max) max = postfix_value;
				}
				
			}
		}
		closedir(directory_pointer);
	}
	
	printf("文件名後綴最大值和最小值:max == %u	min == %u\n",max,min);

	/*****************創建新的文件************************/
	memset( path,'\0',sizeof( path ) );
	memset( path_name_temp,'\0',sizeof( path_name_temp ) );
	strcat(path_name_temp,file_path_profix);
	strcat(path_name_temp,file_name_prefix);
	strcat(path_name_temp,"%u");

	sprintf(path,path_name_temp,max+1);

	FILE *now_file = fopen(path,"w");
	if( now_file  == NULL)
	{
		printf("文件創建失敗!!!\n");
		return -2;
	}
	fclose(now_file);
	
	strcpy(file_path,path);

	/*****************刪除舊的文件*************************/

	if( max - min > 9) //超過10個文件就會自動刪除編號最小的一個;
	{
		memset( path,'\0',sizeof( path ) );
		memset( path_name_temp,'\0',sizeof( path_name_temp ) );
		strcat(path_name_temp,file_path_profix);
		strcat(path_name_temp,file_name_prefix);
		strcat(path_name_temp,"%u");

		sprintf(path,path_name_temp,min);
		
		if( remove(path) != 0 )
		{
			printf("最小文件刪除失敗!!!\n");
			return -3;
		}
		
	}
	return 0;
	
}

int is_in(char *s, char *c)
{
	int i=0,j=0,flag=-1;
	
	while(i<strlen(s) && j<strlen( c ))
	{
		if(s[i] == c[j])
		{
			i++;
			j++;
		}
		else
		{
			i=i-j+1;
			j=0;
		}
		
		if(j==strlen( c ))
		{
			flag=1;
			break;
		}
	}
	return flag;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章