`
haoningabc
  • 浏览: 1465639 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

unix高级编程的部分笔记

    博客分类:
  • c
c 
阅读更多

>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缓存
的刷新操作




分享到:
评论

相关推荐

    UNIX环境高级编程 学习笔记

    学习LINUX环境编程的见证,一笔一画,脉络清楚,结构清晰,自己再看一目了然,传上来与众分享

    unix网络编程读书笔记

    UNIX 网络编程读书笔记 ...本篇读书笔记涵盖了 UNIX 网络编程的基础知识、并发服务器设计思想、TCP/IP 协议、Socket 编程、高级知识点和网络服务器设计等多个方面,对于学习 UNIX 网络编程的读者非常有价值。

    UNIX 高级编程 笔记

    ### UNIX高级编程知识点详解 #### 一、课程体系与学习目标概述 - **语言**: 本课程主要聚焦于两种主流的编程语言:C 和 C++。 - **算法**: 学习经典算法及其优化方法,提高解决复杂问题的能力。 - **数据结构**: ...

    UNIX环境高级编程笔记

    这是Unix环境高级编程所做的笔记。在学习这本书的时候有参考价值。

    unix shell编程第三版笔记

    "Unix Shell编程第三版笔记"是铁道出版社出版的一本教材,它深入浅出地讲解了Unix Shell编程的基础和高级技巧。这份笔记涵盖了从基本的命令行操作到复杂的脚本编写,帮助学习者掌握这一强大的自动化工具。 Unix ...

    UNIX环境高级编程 笔记

    ### UNIX环境高级编程知识点 #### 一、UNIX系统基础与用户信息管理 - **系统目录结构**:UNIX系统采用层次化的目录结构来组织文件。其中,`/etc/passwd` 文件存储了用户的登录信息,包括用户名、用户ID、加密密码...

    UNIX系统编程学习笔记

    在进入UNIX系统编程的学习之前,我们首先需要理解UNIX的基本概念。UNIX是一种多用户、多任务的操作系统,由贝尔实验室在1960年代末开发。它以其简洁、强大的命令行接口和丰富的工具集而闻名,是许多现代操作系统设计...

    《LINUX与UNIX SHELL编程指南》读书笔记

    通过阅读《LINUX与UNIX SHELL编程指南》,读者不仅可以掌握Shell编程的基本技能,还能了解到一些高级特性,如数组、子shell、进程控制等。结合书中提供的实例,读者可以逐步提升自己的Shell编程能力,从而在Linux和...

    LINUX与UNIX_Shell编程指南V1.0_学习笔记.docx

    总的来说,这份"LINUX与UNIX_Shell编程指南"的学习笔记将带你深入探索Shell编程的核心概念,不仅包括基础的文件权限和安全,还可能涉及更高级的主题,如脚本编写技巧、错误处理和调试方法。掌握这些知识,你将成为一...

    UNix系统编程笔记

    通过以上内容,我们可以了解到Unix系统编程涉及到多个方面的知识,从操作系统的基本概念到具体的文件操作、进程管理等高级主题。掌握这些知识点对于深入理解Unix系统的工作原理以及编写高质量的应用程序至关重要。

    Unix环境高级编程apeu加链接库

    在阅读《Unix环境高级编程》的过程中,搭建环境的详细笔记将有助于更好地理解和实践上述概念。这些笔记可能包括安装必要的编译工具、配置环境变量、解决依赖问题以及实际编程示例等步骤,确保读者能够顺利地在实践中...

    笔记_UNIX环境高级编程(第二版).pdf

    笔记_UNIX环境高级编程(第二版)

    Unix 脚本编程总结与应用实例及其他内部资料

    "UNIX.Shell编程24学时教程.[it270.com].pdf"可能是更全面的教程,包括了24个课时的学习内容,覆盖了更高级的主题,如环境变量、输入/输出重定向、管道和子shell等。 "the Art Of Unix Programming.pdf"是著名的...

    LINUX与UNIX SHELL编程指南 读书笔记

    在深入探讨《LINUX与UNIX SHELL编程指南》的读书笔记之前,让我们首先理解Linux和Unix Shell编程的基础概念。Linux是一种自由开源的操作系统,它的内核由林纳斯·托瓦兹开发,而Unix则是一个历史悠久的多用户、多...

Global site tag (gtag.js) - Google Analytics