unix高级编程的部分笔记



>gcc first.c -o first.out
数open、read、write、lseek以及close提供了不用缓存的I / O
./testfile.out /root/haoning
<unistd.h>
getpid();
opendir(ss) 返回DIR 指针
★进程:f o r k、e x e c和w a i t p i d(e x e c函数有六种变体,但经常把
它们统称为e x e c函数
fgets返回的每一行都以新行符终止,后随一个n u l l字节
调用fork创建一个新进程

★★★★★代码
#first.c:
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
printf("ssssss");
exit(0);
}
★★★★★★★★★★★
#second.c
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main(int argc,char *argv[])
{
DIR *dp;
struct dirent *dirp;
if(argc!=2)
printf("error");
if((dp=opendir(argv[1]))==NULL)
printf("errror2");
while((dirp=readdir(dp))!=NULL)
printf("%s\n",dirp->d_name);
closedir(dp);
exit(0);
}
./second.out /root/haoning 第一个参数是参数数量,包含命令,第二个参数是命令参数数组指针
★★★★★★★★
#third.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define BUFFSIZE 8192
int main(void)
{
int n;
char buf[BUFFSIZE];
while((n=read(STDIN_FILENO,buf,BUFFSIZE))>0)
if(write(STDOUT_FILENO,buf,n)!=n)
printf("error");
if(n<0)
printf("error1");
exit(0);
}
./third.out 或者 ./third.out > data
★★★★★★★★★★★★★
#forth.c
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int c;
while((c=getc(stdin))!=EOF)
if(putc(c,stdout)==EOF)
printf("error");
if(ferror(stdin))
printf("error2");
exit(0);
}
跟上面一样
★★★★★★★★
#fifth.c
#include <stdio.h>
#include <stdlib.h>
int main(void){
printf("pid:%d\n",getpid());
exit(0);
}
注意c#的写法
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
调用系统命令的程序,
#seven.c开始不好使 需要加apue.h
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "apue.h"
int main(void)
{
char buf[MAXLINE];
pid_t pid;
int status;
printf("%%");
while(fgets(buf,MAXLINE,stdin)!=NULL){
buf[strlen(buf)-1]=0;
if((pid=fork())<0)//调用了
printf("fork error");
else if(pid==0){//也调用了
execlp(buf,buf,(char *) 0);
exit(127);
}
if((pid=waitpid(pid,&status,0))<0)
printf("waitpid error");
printf("%%");
}
exit(0);
}
seven.c: In function ‘main’:
seven.c:8: error: ‘MAXLINE’ undeclared (first use in this function)
seven.c:8: error: (Each undeclared identifier is reported only once
seven.c:8: error: for each function it appears in.)
seven.c:13: warning: incompatible implicit declaration of built-in function ‘strlen
我需要知道,方法都在哪个库里面,查“C语言函数速查.chm” strlen属于string.h 但是MAXLINE在哪????
★★★★★★★★★★★

ssize_t read(int, void *, size_t);
ssize_t write(int, const void *, size_t);
pid_t getpid(void);

第三章:
lseek函数:"当前文件位移量"
od -c file.hole
使用o d ( 1 )命令观察该文件的实际内容。命令行中的- c标志表示以字符方式打印文件内容
#rumen714/third.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include "apue.h"
char buf1[] ="abcdefghij";
char buf2[]="ABCDEFGHIJ";
int main(void)
{
int fd;
if((fd=creat("file.hole",FILE_MODE))<0)
printf("create error");
if(write(fd,buf1,10)!=10)
printf("buf1 write error");
if(lseek(fd,40,SEEK_SET)==1)
printf("fleek error");
if(write(fd,buf2,10)!=10)
printf("buf2 write error");
exit(0);
}
int =creat(文件名,FILE_MODE);
write(int,字符数组,长度);
lseek(int,长度,seek_set)
★★★★★★★★★★★★★★★★★★★★★
当打开一个流时,标准I/O函数fopen返回一个指向FILE对象的指针(struct)
文件描述符:STDIN_FILENO,STDOUT_FILENO,STDERR_FILENO
预定义文件指针:stdin,stdout,stderr,
★★★★★★★★★★★★★★★★★★★★★★★
★怎么进入另一个线程? ★
★运行的tomcat,main怎么进入获取tomcat的jndi★
★★★★★★★★★★★★★★★★★★★★★★★
全缓存,行缓存,不带缓存
#include <stdio.h>
void setbuf(FILE fp*, char *buf) ;
int setvbuf(FILE fp,* char *buf, int mode, size_t size) ;
_IOFBF 全缓存
_IOLBF 行缓存
_IONBF 不带缓存

强制刷新一个缓存
# include<stdio.h>
int fflush(FILEf p*);

打开流:
#include <stdio.h>
FILE *fopen(const char *pathname, const char *type) ;
FILE *freopen(const char *pathname, const char *type, FILE *fp) ;
FILE *fdopen(int filedes, const char *type) ;取一个现存的文件描述符(我们可能从open,dup ,dup2 ,fcntl或pipe函数得到此文件描述符)
type:
r 或r b 为读而打开
w 或w b 使文件成为0长,或为写而创建
a 或a b 添加;为在文件尾写而打开,或为写而创建
r+ 或r+b 或r b + 为读和写而打开
w+ 或w+b 或w b + 使文件为0长,或为读和写而打开
a+ 或a+b 或a b + 为在文件尾读和写而打开或创建

★一次读一个字符
#include <stdio.h>
int getc(FILE fp*) ; 宏,更快
int fgetc(FILE fp*) ; funnctiongetc 函数
int getchar(void); =getc(stdin)

#include <stdio.h>
int ferror(FILE fp*) ;
int feof(FILE fp*) ;
两个函数返回:若条件为真则为非0(真),否则为0(假)
void clearerr(FILEf p)* ;)

#include <stdio.h>
int ungetc(int c, FILE *fp)

#include <stdio.h>
int putc(int c , FILE *fp);
int fputc(int c, FILE *fp);
int putchar(int c); =putc(c,stdout)

★每次一行I/O (FILE *fp 可以用stdin)
#include <stdio.h>
char *fgets(char *buf, int n,FILE *fp) ;(不推荐使用)
char *gets(char *buf);

#include <stdio.h>
int fputs(const char str*, FILE *fp) ;
int puts(const char str*) ;

第8章
_ e x i t并不执行标准I / O缓存
的刷新操作


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值