|
|
50鱼币
希望大神们指导
档案建立后 无法新增档案
会以同一个文件持续下去 且覆盖原文件内容
我想要做到文件比对
若文件存在
则创新文件
若不存在
也创新文件
但每次只创建一个新文件
内部内容可以直接写入任意值
档名以流水号形式增加
example :
file_001
file_002
file_003.
.
.
.
file_020
你看看这个呢?? - #include <stdio.h>
- #include <stdlib.h>
- #include <io.h>
- #include <string.h>
- char *substring(char *dst,char *src,int start)
- {
- char *p=dst;
- char *q=src;
- int length=strlen(src);
- int dstlen=length-4-4;
- if(start>=length||start<0 || dstlen<1)
- return NULL;
- q+=start;
- while(dstlen--)
- {
- *(p++)=*(q++);
- }
- *(p++)='\0';
- return dst;
- }
- char* getfilename( const char * path,char* dst )
- {
- struct _finddata_t data;
- char tmp[256]={"\0"};
- long hnd = _findfirst( path, &data );
- if ( hnd < 0 )
- {
- return NULL;
- }
- int nRet = (hnd <0 ) ? -1 : 1;
- int max=0;
- while ( nRet >= 0 )
- {
- if ( data.attrib!= _A_SUBDIR ){
- int t= atoi(substring(tmp,data.name,4));
- if (t>max) max=t;
- }
- nRet = _findnext( hnd, &data );
- }
- _findclose( hnd );
- sprintf(dst,"file%04d.txt",max+1);
- return dst;
- }
- int main()
- {
- char filename[256]={"\0"};
- char *file=getfilename(".\\file*.txt",filename);
- FILE *fp;
- if (file){
- fp = fopen(file, "w");
- printf("%s文件创建成功\n",file);
- }else
- {
- fp=fopen("file0001.txt","w");
- printf("初始文件创建成功\n");
- }
- fclose(fp);
- return 0;
-
- }
- /*
- PS D:\我> ./wp1
- 初始文件创建成功
- PS D:\我> ./wp1
- file0002.txt文件创建成功
- PS D:\我> ./wp1
- file0003.txt文件创建成功
- PS D:\我> ./wp1
- file0004.txt文件创建成功
- PS D:\我> ./wp1
- file0005.txt文件创建成功
- PS D:\我> ./wp1
- file0006.txt文件创建成功
- PS D:\我>
- */
复制代码
|
|