Linux編程學習三
在linux中,任何設備,文件,目錄...都是以文件的形式存放的。
1.比較重要的設備文件:
/dev/console 系統控制檯,出錯和診斷信息通常會被髮送到這裏
/dev/tty 進程控制終端(鍵盤和顯示器)的一個假名,如ls -R | more,需要鍵盤操作才能顯示下一頁
/dev/null 空設備,所有寫向這個設備的輸出都將被拋棄。
如: $ echo do not want to see this >/dev/null
$ cp /dev/null empty_file
2.系統底層訪問
#include <unistd.h>
#include <stdlib.h>
write系統調用:
size_t write(int fildes, const void *buf, size_t nbytes);
fildes:文件描述符
*buf: 緩衝區
nbytes: 字節數
把緩衝區buf裏的前nbytes個字節寫入與文件描述符filds相關的文件中去。
返回實際寫入的字節數,返回0表示沒寫入,返回-1表示出錯
read系統調用:
size_t read(int fildes, const void *buf, size_t nbytess);
從文件描述符fildes相關的文件裏讀入nbytes個字節的數據並把它放在數據區buf中去。
其他與write同。
open系統調用:
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);
open建立一條到文件或設備的訪問路徑,返回一個文件描述符,供write和read調用。
oflags: O_APPEND, O_TRUNC, O_CREAT, O_EXCL
mode中標誌位按OR操作後得到的:
S_IRUSR,S_IWUSR, S_IXUSR...(USR,GRP,OTH);
如: open("myfile", O_CREAT, S_IRUSR | S_IXOTH);
$ls -ls myfile
0 -r------x 1 song software 0 sep 21 06:34 myfile*
umask變量:對文件的訪問權限設定一個掩碼。
close系統調用:
int close (int fildes);
ioctl系統調用:
int ioctl(int fildes,int cmd,...);
提供對設備訪問等方面進行控制的操作界面。
lseek, fstat, stat, lstat, dup和dup2系統調用;
例子:
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main()
{
char c;
int in, out;
in=open("file.in",O_RDONLY);
out=open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
while(read(in,&c,1)==1)
write(out,&c,1);
exit (0);
}
3.標準I/O庫
#include <stdio.h>
FILE *fopen(const char *filename, const char *mode);
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
size_t fwrite(const void *ptr, sizet size, size_t nitems, FILE *stream);
int fclose(FILE *stream);
int fflush(FILE *stream);
int fseek(FILE *stream, ong int offset, int whence);
.....
int printf(const char *format, ....);
int scanf(const char *format, ...);
int ferror(FILE *stream);
int feof(FILE *stream);
void clearerr(FILE *stream);
4.文件和子目錄的維護
#include <sys/stat.h>
int chmod(const char *path, mode_t mode);
int chown(const char *path, uid_t owner, gid_t group);
mkdir,rmdir,opendir,closedir,readdir,telldir,seekdir...
1.比較重要的設備文件:
/dev/console 系統控制檯,出錯和診斷信息通常會被髮送到這裏
/dev/tty 進程控制終端(鍵盤和顯示器)的一個假名,如ls -R | more,需要鍵盤操作才能顯示下一頁
/dev/null 空設備,所有寫向這個設備的輸出都將被拋棄。
如: $ echo do not want to see this >/dev/null
$ cp /dev/null empty_file
2.系統底層訪問
#include <unistd.h>
#include <stdlib.h>
write系統調用:
size_t write(int fildes, const void *buf, size_t nbytes);
fildes:文件描述符
*buf: 緩衝區
nbytes: 字節數
把緩衝區buf裏的前nbytes個字節寫入與文件描述符filds相關的文件中去。
返回實際寫入的字節數,返回0表示沒寫入,返回-1表示出錯
read系統調用:
size_t read(int fildes, const void *buf, size_t nbytess);
從文件描述符fildes相關的文件裏讀入nbytes個字節的數據並把它放在數據區buf中去。
其他與write同。
open系統調用:
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);
open建立一條到文件或設備的訪問路徑,返回一個文件描述符,供write和read調用。
oflags: O_APPEND, O_TRUNC, O_CREAT, O_EXCL
mode中標誌位按OR操作後得到的:
S_IRUSR,S_IWUSR, S_IXUSR...(USR,GRP,OTH);
如: open("myfile", O_CREAT, S_IRUSR | S_IXOTH);
$ls -ls myfile
0 -r------x 1 song software 0 sep 21 06:34 myfile*
umask變量:對文件的訪問權限設定一個掩碼。
close系統調用:
int close (int fildes);
ioctl系統調用:
int ioctl(int fildes,int cmd,...);
提供對設備訪問等方面進行控制的操作界面。
lseek, fstat, stat, lstat, dup和dup2系統調用;
例子:
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main()
{
char c;
int in, out;
in=open("file.in",O_RDONLY);
out=open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
while(read(in,&c,1)==1)
write(out,&c,1);
exit (0);
}
3.標準I/O庫
#include <stdio.h>
FILE *fopen(const char *filename, const char *mode);
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
size_t fwrite(const void *ptr, sizet size, size_t nitems, FILE *stream);
int fclose(FILE *stream);
int fflush(FILE *stream);
int fseek(FILE *stream, ong int offset, int whence);
.....
int printf(const char *format, ....);
int scanf(const char *format, ...);
int ferror(FILE *stream);
int feof(FILE *stream);
void clearerr(FILE *stream);
4.文件和子目錄的維護
#include <sys/stat.h>
int chmod(const char *path, mode_t mode);
int chown(const char *path, uid_t owner, gid_t group);
mkdir,rmdir,opendir,closedir,readdir,telldir,seekdir...
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.