Você está na página 1de 8

system()

system("a.exe -c i -g j");
i j i j
system() a.exe i j -c -g

system
char cmdstr[100];
int i,j;
i=6;
j=6;
sprintf(cmdstr,"a.exe -c %d -g %d",i,j);
system(cmdstr);

tmpfile
1
0

gdb tmpfile()
/tmp API

linux-0gt0:~ # cat 1.c


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
pid_t pid;
char s[100];
FILE* f;
f=tmpfile();
pid=getpid();
sprintf(s,"ls -l /proc/%d/fd",pid);
system(s);
return 0;
}
linux-0gt0:~ # gcc 1.c && ./a.out
total 4
lrwx------ 1 root root 64 Aug 30 04:55 0 -> /dev/pts/0
lrwx------ 1 root root 64 Aug 30 04:55 1 -> /dev/pts/0
lrwx------ 1 root root 64 Aug 30 04:55 2 -> /dev/pts/0
lrwx------ 1 root root 64 Aug 30 04:55 3 -> /tmp/tmpfdSnQSE (deleted)
tmpfile /tmp open
unlink close

unlink() unlink fclose() system()


fclose()(deleted)

deleted unlink

linux-0gt0:/tmp # cat 1.c


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int fd;
char s[100];
pid_t pid;
pid=getpid();
sprintf(s,"ls -l /proc/%d/fd",pid);
fd=open("/tmp/mytestfile",O_CREAT,O_WRONLY,0600);
unlink("/tmp/mytestfile");
printf("ls -l /tmp/mytestfile\n");
system("ls -l /tmp/mytestfile");
printf("\n=============================\n");
printf("%s\n",s);
system(s);
close(fd);
printf("\n=============================\n");
printf("%s\n",s);
system(s);
printf("\n=============================\n");
printf("ls -l /tmp/mytestfile\n");
system("ls -l /tmp/mytestfile");
return 0;
}
linux-0gt0:/tmp # gcc 1.c && ./a.out
ls -l /tmp/mytestfile
ls: /tmp/mytestfile: No such file or directory

=============================
ls -l /proc/15843/fd
total 4
lrwx------ 1 root root 64 Aug 30 08:13 0 -> /dev/pts/0
lrwx------ 1 root root 64 Aug 30 08:13 1 -> /dev/pts/0
lrwx------ 1 root root 64 Aug 30 08:13 2 -> /dev/pts/0
lr-x------ 1 root root 64 Aug 30 08:13 3 -> /tmp/mytestfile (deleted)

=============================
ls -l /proc/15843/fd
total 3
lrwx------ 1 root root 64 Aug 30 08:13 0 -> /dev/pts/0
lrwx------ 1 root root 64 Aug 30 08:13 1 -> /dev/pts/0
lrwx------ 1 root root 64 Aug 30 08:13 2 -> /dev/pts/0

=============================
ls -l /tmp/mytestfile
ls: /tmp/mytestfile: No such file or directory

This function removes the directory entry and decrements the link count of the file referenced by
pathname. If there are other links to the file, the data in the file is still accessible through the other
links. The file is not changed if an error occurs.

Only when the link count reaches 0 can the contents of the file be deleted. One other condition
prevents the contents of a file from being deleted: as long as some process has the file open, its
contents will not be deleted. When a file is closed, the kernel first checks the count of the number
of processes that have the file open. If this count has reached 0, the kernel then checks the link
count; if it is 0, the file's contents are deleted.

This property of unlink is often used by a program to ensure that a temporary file it creates won't
be left around in case the program crashes. The process creates a file using either open or creat and
then immediately calls unlink. The file is not deleted, however, because it is still open. Only when
the process either closes the file or terminates, which causes the kernel to close all its open files, is
the file deleted.

I / O tmpnam
#include <stdio.h>
char *tmpnam(char *s);
tmpnam()
TMP_MAX

tmpnam() s NULL s NULL


L_tmpnam

tmpnam

#include <stdio.h>
#define N 128
int main() {
char s[N] = {0}, *p;
if ((p = tmpnam(s)) == NULL) {
perror("tmpnam");
return -1;
}
printf("s=%s p=%s\n", s, p);
return 0;
}

tmpfile :
#include <stdio.h>
FILE *tmpfile(void);

#include <stdio.h>
#define N 128
int main() {
char s[N] = {0}, *p;
FILE * fp;
int ch = 0;
if ((p = tmpnam(s)) == NULL) {
perror("tmpnam");
return -1;
}
printf("s=%s p=%s\n", s, p);
printf("TMP_MAX=%d\n", TMP_MAX);
if ((fp = tmpfile()) == NULL) {
perror("tmpfile");
return -1;
}
if (fputc('a', fp) == EOF) {
perror("fputc");
return -1;
}
fseek(fp, 0, SEEK_SET);
ch = fgetc(fp);
printf("ch=%d\n",ch);
getchar();
puts("end main");
fclose(fp);
ch = fgetc(fp);
printf("ch=%d\n",ch);
return 0;
}

getchar
/tmp deleted /tmp
Linux 0
0

tmpfile()
tmpnam()
fopen()
unlink()
tmpfile()
C fgets(...) fputs(...)
ANSI C 3
1. standard input . stdin.
2 standard output. stdout
3. standard error. stderr.
FILE
fputs(...)
int fputs(char *s, FILE *stream);
s

example
FILE *fp=fopen("test.txt","w");
char s1[20]="hello world";
char *s2="hello C
fputs(s1,fp); //
fputs(s2,fp); //
fputs("hello",fp); //
C

0, 1.
EOF
fputs(char *s, FILE *stream)

fputs(...)
FILE stdout

fputs("hello world",stdout);
hello word.
fputs(...)
int fputs(char *s, FILE *stream)
{
int c;
while(c =*s++) //fputs
putc(c,stream);
return ferror(stream)? EOF:
}

FILE *tmpfile(void); Returns: file pointer if OK, NULL on error.


The tmpfile() function opens a unique temporary file in binary read/write (w+b) mode. The file
will be automatically deleted when it is closed or the program terminates.
int mkstemp(char *template); Returns: file descriptor if OK, 1 on error.
The mkstemp() function generates a unique temporary filename from template. The last six charac
ters of template must be XXXXXX and these are replaced with a string that makes the filename
unique. The file is then created with mode read/write and permissi ons 0666 (glibc 2.0.6 and earl
ier), 0600 (glibc 2.0.7 and later). Since it will be modified, template must not be a string const
ant, but should be declared as a character array. The file is opened with the open(2) O_EXCL flag,
guaranteeing that when mkstemp() returns successfully we are the only user.

tempfile mkstemp
unlink mkstemp unlink
mkstemp

The reason I ask is that I'd like to use tmpfile() to create a temporary file (because then it will be
automatically deleted should my script abruptly terminate.) Once I've written my data to this file, I
then need to pass the filename on to an external command, but I can't see any way to find out what
the temporary file is called.

I've tried using fstat(), but this returns everything except the filename.

You can't. But then the file will be deleted when you fclose() it and you should close a file you
have open for writing before passing it off to another program, anyway.

const char *name = tmpnam(NULL); // Get temp name


FILE *fp = fopen(name, "w"); // Create the file
// ...
fclose(fp);
remove(name);

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int fd;
char temp_file[]="tmp_XXXXXX";
/*Creat a temp file.*/
if((fd=mkstemp(temp_file))==-1)
{
printf("Creat temp file faile./n");
exit(1);
}
/*Unlink the temp file.*/
unlink(temp_file);
/*Then you can read or write the temp file.*/
//ADD YOUR CODE;
/*Close temp file, when exit this program, the temp file will be removed.*/
close(fd);
}

void writeFile() {
char s[] = "abc.txt";
//
int fd = open(s, O_RDWR | O_APPEND);
if(fd == -1) {
// errno
// strerror
printf("error is %s\n", strerror(errno));
return;
}

printf("sucess fd = %d\n", fd);


char buf[100];
memset(buf, 0, sizeof(buf));
int i = 10;
while(i-- > 0){
strcpy(buf, "hello world linux write file \n");
write(fd, buf, strlen(buf));
memset(buf, 0, sizeof(buf));
}

close(fd);

Você também pode gostar

  • WL WS 1
    WL WS 1
    Documento3 páginas
    WL WS 1
    liuyl
    Ainda não há avaliações
  • LNX NFS 1
    LNX NFS 1
    Documento9 páginas
    LNX NFS 1
    liuyl
    Ainda não há avaliações
  • Sed 2
    Sed 2
    Documento22 páginas
    Sed 2
    liuyl
    Ainda não há avaliações
  • Hp-Ux KC 10
    Hp-Ux KC 10
    Documento1 página
    Hp-Ux KC 10
    liuyl
    Ainda não há avaliações
  • Sed 1
    Sed 1
    Documento11 páginas
    Sed 1
    liuyl
    Ainda não há avaliações
  • LNX KM 1
    LNX KM 1
    Documento5 páginas
    LNX KM 1
    liuyl
    Ainda não há avaliações
  • Hp-Ux Q4 1
    Hp-Ux Q4 1
    Documento10 páginas
    Hp-Ux Q4 1
    liuyl
    Ainda não há avaliações
  • Data SS0
    Data SS0
    Documento4 páginas
    Data SS0
    liuyl
    Ainda não há avaliações
  • TC 0
    TC 0
    Documento2 páginas
    TC 0
    liuyl
    Ainda não há avaliações
  • Hds Emc迁移测试方案
    Hds Emc迁移测试方案
    Documento4 páginas
    Hds Emc迁移测试方案
    liuyl
    Ainda não há avaliações
  • HDS Health Check 1
    HDS Health Check 1
    Documento4 páginas
    HDS Health Check 1
    liuyl
    Ainda não há avaliações
  • Program Mmap
    Program Mmap
    Documento5 páginas
    Program Mmap
    liuyl
    Ainda não há avaliações
  • Data SS1
    Data SS1
    Documento17 páginas
    Data SS1
    liuyl
    Ainda não há avaliações
  • LNX KM 1
    LNX KM 1
    Documento5 páginas
    LNX KM 1
    liuyl
    Ainda não há avaliações
  • SDUX2
    SDUX2
    Documento3 páginas
    SDUX2
    liuyl
    Ainda não há avaliações
  • Win Repair
    Win Repair
    Documento3 páginas
    Win Repair
    liuyl
    Ainda não há avaliações
  • Win Repair
    Win Repair
    Documento3 páginas
    Win Repair
    liuyl
    Ainda não há avaliações
  • SW 2
    SW 2
    Documento3 páginas
    SW 2
    liuyl
    Ainda não há avaliações
  • 40F Backup 1
    40F Backup 1
    Documento4 páginas
    40F Backup 1
    liuyl
    Ainda não há avaliações
  • 40F Backup 2
    40F Backup 2
    Documento3 páginas
    40F Backup 2
    liuyl
    Ainda não há avaliações
  • SDUX1
    SDUX1
    Documento4 páginas
    SDUX1
    liuyl
    Ainda não há avaliações
  • Aix Mem Faq
    Aix Mem Faq
    Documento3 páginas
    Aix Mem Faq
    liuyl
    Ainda não há avaliações
  • Aix Pha Faq
    Aix Pha Faq
    Documento1 página
    Aix Pha Faq
    liuyl
    Ainda não há avaliações
  • Adv FS1
    Adv FS1
    Documento2 páginas
    Adv FS1
    liuyl
    Ainda não há avaliações
  • SW 1
    SW 1
    Documento1 página
    SW 1
    liuyl
    Ainda não há avaliações
  • Program Make 8
    Program Make 8
    Documento10 páginas
    Program Make 8
    liuyl
    Ainda não há avaliações
  • Program Make 9
    Program Make 9
    Documento7 páginas
    Program Make 9
    liuyl
    Ainda não há avaliações
  • Program Make 5a
    Program Make 5a
    Documento26 páginas
    Program Make 5a
    liuyl
    Ainda não há avaliações
  • Program Make 7
    Program Make 7
    Documento66 páginas
    Program Make 7
    liuyl
    Ainda não há avaliações
  • Program Make 7
    Program Make 7
    Documento66 páginas
    Program Make 7
    liuyl
    Ainda não há avaliações
  • 五年级教学详案相声
    五年级教学详案相声
    Documento4 páginas
    五年级教学详案相声
    edeline
    Ainda não há avaliações
  • 万善先资集 11061005
    万善先资集 11061005
    Documento235 páginas
    万善先资集 11061005
    echosor green
    Ainda não há avaliações
  • Tugasan
    Tugasan
    Documento3 páginas
    Tugasan
    Sandy Chan
    Ainda não há avaliações
  • 0
    0
    Documento7 páginas
    0
    Xam Hsc
    Ainda não há avaliações
  • C 62 L
    C 62 L
    Documento13 páginas
    C 62 L
    Aileen Yan
    Ainda não há avaliações