Escolar Documentos
Profissional Documentos
Cultura Documentos
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
deleted unlink
=============================
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
#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:
}
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.
#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;
}
close(fd);