创建临时文件mkstemp函数和 tmpfile函数

#include 
#include "apue.h"
#include 
#include 
#include 
#include 

int main() {
    char name[L_tmpnam] = {"/tmp/fileXXXXXX"};
    char line[MAXLINE];
    char name2[L_tmpnam] = {"/tmp/fileXXXXXX"};

    FILE * fp;
    if(mkstemp(name) != -1){

    }
    else{
        printf("create tmp file failed.\n");
    }
    printf("%s\n",name);

    if(mkstemp(name2) != -1){

    }
    else{
        printf("create tmp file2 failed.\n");
    }

    printf("%s\n",name2);

    if((fp = tmpfile()) == NULL){
       printf("tmpfile error.\n");

    }
    fputs("one line of out put\n",fp);
    rewind(fp);
    if(fgets(line,sizeof(line),fp) == NULL){
       printf("fgets error\n");
    }
    fputs(line,stdout);

    return 0;
}

输出

/tmp/fileLYatYm
/tmp/fileFPjSH2
one line of out put

Process finished with exit code 0
 

你可能感兴趣的:(C,linux)