3dmax临时文件怎么重命名 (mv重命名文件生成临时文件)

C输入输出库函数除了操作文件以外,还可以操作目录(文件夹):

1 指定一个目录为当前目录

#include <direct.h>  // vc, TC: dir.h
#include <stdio.h>
#define MAX_PATH 255

int main()
{
    char path[MAX_PATH];
    getcwd(path,MAX_PATH);      // 获取当前工作目录
    printf("%s\n",path);
    if(chdir("d:")==0){         // 改变当前目录
        getcwd(path,MAX_PATH);  // 获取当前目录
        printf(path);
    }
    getchar();
}

2 新建、删除目录(文件夹)

#include <direct.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{
    rmdir("c:\\test");          // 删除文件夹
    if(mkdir("c:\\test")==0){   // 新建目录
        system("start c:\\test");
    }
    getchar();
}

3 查找指定文件

#include <io.h>
#include <stdio.h>

int main()
{
    // 文件查找
    long handle;
    struct _finddata_t fileInfo;
    if((handle=_findfirst("D:\\*.txt",&fileInfo))==-1L)
        printf("没有找到匹配的项目\n");
    else
    {
        printf("%d\n",handle);
        printf("%s\n",fileInfo.name);
        while(_findnext(handle,&fileInfo)==0)
            printf("%s\n",fileInfo.name);
        _findclose(handle);
    }
    getchar();
}

4 创建临时文件

创建名字唯一的临时文件:推荐使用tmpfile和mkstemp,因为这两个函数创建文件的同时并打开文件,总的来说安全又可靠。tmpfile返回的是文件流指针FILE*,mkstemp返回的是文件描述符。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char tmpname[L_tmpnam];
    char *filename;
    FILE *tmpfp;
    
    if((filename = tmpnam(tmpname)) != NULL)    //方式一:不推荐使用
    {
        printf("Temporary file name is: %s\n", filename);
        printf("Temporary  tmpnam   is: %s\n", tmpname);//在一次打开中filename 与 tmpnam相同
        
        fopen(tmpname,"r");
        perror("open");
        
        fopen(filename,"r");
        perror("open");
    }
    
    tmpfp = tmpfile();  //方式二:推荐使用
    if(tmpfp)
        printf("Opened a temporary file OK\n");
    else
        perror("tmpfile");
    
    getchar();
}
/*
Temporary file name is: \s5tg.
Temporary  tmpnam   is: \s5tg.
open: No such file or directory
open: No such file or directory
Opened a temporary file OK
*/

mktemp()用法与tmpnam差不多,只是名字部分可以由template确定,template后面六个字符必须为X,例如tmpXXXXXX。

#include <stdlib.h>
#include <stdio.h>
#include <io.h>
//char *mktemp(char *template);

int main(int argc,char *argv[])
{
    char filename[] = "temp-XXXXXX";
    char *name;
    if((name = mktemp(filename)) == NULL)
    {
        perror("mktemp");
        exit(1);
    }
    
    printf("name:%s\nfilename:%s\n",name,filename);
    getchar();
}
/*
name:temp-a04108
filename:temp-a04108
*/

建立多个:

#include <io.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main()
{
    // 创建临时文件
    char *templat = "fnXXXXXX";
    char *result;
    char names[5][9];
    
    int i;
    FILE *fp;
    
    for( i = 0; i < 5; i++ )
    {
        strcpy( names[i], templat );
        result = _mktemp( names[i] );  
        // Note: _mktemp is deprecated; consider using _mktemp_s instead
        if( result == NULL )
        {
            printf( "Problem creating the template\n" );
            if (errno == EINVAL)
            {
                printf( "Bad parameter\n");
            }
            else if (errno == EEXIST)
            {
                printf( "Out of unique filenames\n"); 
            }
        }
        else
        {
            fp = fopen(result, "w" );
            if( fp != NULL )
                printf( "Unique filename is %s\n", result );
            else
                printf( "Cannot open %s\n", result );
            fclose( fp );
        }
    }
    getchar();
}

与mktemp函数区别:mkstemp返回值为文件描述符,mktemp返回值为文件名指针。

#include <stdlib.h>
#include <stdio.h>
#include <io.h>
int main()
{
    int fd;
    char tmpfile[] = "temp-XXXXXX";
    
    if((fd = mkstemp(tmpfile))< 0)
    {
        perror("mkstemp");
        exit(1);
    }
    
    close(fd);
    exit(0);
}

5 文件重命名

#include <stdio.h>

int main(void)
{
    char oldname[80], newname[80];
    
    /* prompt for file to rename and new name */
    printf("File to rename: ");
    gets(oldname);
    printf("New name: ");
    gets(newname);
    
    /* Rename the file */
    if (rename(oldname, newname) == 0)
        printf("Renamed %s to %s.\n", oldname, newname);
    else
        perror("rename");
    getchar();
    return 0;
}
/*
File to rename: d:\test.txt
New name: d:\demo.txt
Renamed d:\test.txt to d:\demo.txt.
*/

6 文件删除

#include <stdio.h>

int main(void)
{
    char file[80];
    
    /* prompt for file name to delete */
    printf("File to delete: ");
    gets(file);
    
    /* delete the file */
    if (remove(file) == 0)
        printf("Removed %s.\n",file);
    else
        perror("remove");
    getchar();
    return 0;
}
/*
File to delete: d:\demo.txt
Removed d:\demo.txt.
*/

ref:

https://www.apiref.com/c-zh/h_dir.htm