系統(tǒng)調(diào)用
- 系統(tǒng)調(diào)用主要進(jìn)行進(jìn)程控制(系統(tǒng)調(diào)用是系統(tǒng)提供用戶的一個(gè)接口)
- flag:O_RDONLY,O_WRONLY,O_RDWR
- 路徑:
- 絕對(duì)路徑:以/開始的路徑稱之為絕對(duì)路徑/user/inclue
- 相對(duì)路徑:以./開始的路徑稱之為相對(duì)路徑,./可以省略不寫./..1612-->../1612
- 使用flag指定的方式打開指定路徑的文件
- 若文件打開成功,返回該文件的一個(gè)新的文件描述符
- 文件描述符用于文件操作
- 若文件打開失敗,返回-1
- pathname:"./stu.info"
- 一個(gè)文件的創(chuàng)建,以及文件的屬性
//1_read_only.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>//錯(cuò)誤號(hào)erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路徑:
//絕對(duì)路徑:以/開始的路徑稱之為絕對(duì)路徑
//相對(duì)路徑:以./開始的路徑稱之為相對(duì)路徑,./可以省略不寫
//使用flag指定的方式打開指定路徑的文件
//若文件打開成功,返回該文件的一個(gè)新的文件描述符
//文件描述符用于文件操作
//若文件打開失敗,返回-1
//int open(const char *pathname,int flag);
int main()
{
int fd = -1;
//以只讀方式打開文件,若文件不存在,不會(huì)自動(dòng)創(chuàng)建文件,打開失敗
fd=open("stu.info",O_RDONLY);
if(-1==fd)
{
printf("open file failed...\n");
//當(dāng)打開失敗,會(huì)對(duì)errno設(shè)置錯(cuò)誤值
//該錯(cuò)誤值代表發(fā)生的錯(cuò)誤
printf("errno=%d\n",errno);
//將strerror獲得erron代表的錯(cuò)誤信息
printf("erron:%s\n",strerror(errno));
}
else
{
printf("open file ok...\n");
//關(guān)閉打開的文件描述符:關(guān)閉文件
close(fd);
}
return 0;
}
//1_write_only.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>//錯(cuò)誤號(hào)erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路徑:
//絕對(duì)路徑:以/開始的路徑稱之為絕對(duì)路徑
//相對(duì)路徑:以./開始的路徑稱之為相對(duì)路徑,./可以省略不寫
//使用flag指定的方式打開指定路徑的文件
//若文件打開成功,返回該文件的一個(gè)新的文件描述符
//文件描述符用于文件操作
//若文件打開失敗,返回-1
//int open(const char *pathname,int flag);
int main()
{
int fd = -1;
//以只讀方式打開文件,若文件不存在,不會(huì)自動(dòng)創(chuàng)建文件,打開失敗
fd=open("stu.info",O_RDWR);
if(-1==fd)
{
printf("open file failed...\n");
//當(dāng)打開失敗,會(huì)對(duì)errno設(shè)置錯(cuò)誤值
//該錯(cuò)誤值代表發(fā)生的錯(cuò)誤
printf("errno=%d\n",errno);
//將strerror獲得erron代表的錯(cuò)誤信息
printf("erron:%s\n",strerror(errno));
}
else
{
printf("open file ok...\n");
//關(guān)閉打開的文件描述符:關(guān)閉文件
close(fd);
}
return 0;
}
//create file.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>//錯(cuò)誤號(hào)erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路徑:
//絕對(duì)路徑:以/開始的路徑稱之為絕對(duì)路徑
//相對(duì)路徑:以./開始的路徑稱之為相對(duì)路徑,./可以省略不寫
//使用flag指定的方式打開指定路徑的文件
//若文件打開成功,返回該文件的一個(gè)新的文件描述符
//文件描述符用于文件操作
//若文件打開失敗,返回-1
//int open(const char *pathname,int flag);
//mode:
// S_IRWXU:用戶對(duì)文件具有讀,寫,執(zhí)行的權(quán)限
// S_IRUSR:用戶對(duì)文件具有讀的權(quán)限
// S_IWUSR:用戶對(duì)文件具有寫的權(quán)限
// S_IXUSR:用戶對(duì)文件具有執(zhí)行的權(quán)限
// S_IRWXG:用戶對(duì)文件具有讀,寫,執(zhí)行的權(quán)限
// S_IRGRP:用戶對(duì)文件具有讀的權(quán)限
// S_IWGRP:用戶對(duì)文件具有寫的權(quán)限
// S_IXGRP:用戶對(duì)文件具有執(zhí)行的權(quán)限
// S_IRWXO:用戶對(duì)文件具有讀,寫,執(zhí)行的權(quán)限
// S_IROTH:用戶對(duì)文件具有讀的權(quán)限
// S_IWOTH:用戶對(duì)文件具有寫的權(quán)限
// S_IXOTH:用戶對(duì)文件具有執(zhí)行的權(quán)限
//當(dāng)需要指定多個(gè)權(quán)限的時(shí)候,
//使用mode指定的文件權(quán)限的方式在指定的路徑下創(chuàng)建文件
//int create (const char *pathname,mode_t mode);
int main()
{
int fd = -1;
//以只讀方式打開文件,若文件不存在,不會(huì)自動(dòng)創(chuàng)建文件,打開失敗
fd=open("stu.info",O_RDONLY);
if(-1==fd)
{
printf("open file failed...\n");
//當(dāng)打開失敗,會(huì)對(duì)errno設(shè)置錯(cuò)誤值
//該錯(cuò)誤值代表發(fā)生的錯(cuò)誤
printf("errno=%d\n",errno);
//將strerror獲得erron代表的錯(cuò)誤信息
printf("erron:%s\n",strerror(errno));
if(2==errno)
{
//以用戶具有讀寫權(quán)限,用戶組具有讀權(quán)限
//其他用戶具有讀權(quán)限的方式創(chuàng)建文件
//相當(dāng)于創(chuàng)建一個(gè)文件,并且以寫的方式打開該文件
//并且將該文件內(nèi)容截?cái)啵ㄇ蹇瘴募? fd=creat("stu.info",s_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if(-1==fd)
{
printf("erron=%s\n",strerror(errno));
}
else
{
printf("create file ok\n");
}
}
}
else
{
printf("open file ok...\n");
//關(guān)閉打開的文件描述符:關(guān)閉文件
close(fd);
}
return 0;
}
//2create_file.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>//錯(cuò)誤號(hào)erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路徑:
//絕對(duì)路徑:以/開始的路徑稱之為絕對(duì)路徑
//相對(duì)路徑:以./開始的路徑稱之為相對(duì)路徑,./可以省略不寫
//使用flag指定的方式打開指定路徑的文件
//若文件打開成功,返回該文件的一個(gè)新的文件描述符
//文件描述符用于文件操作
//若文件打開失敗,返回-1
//int open(const char *pathname,int flag);
//mode:
// S_IRWXU:用戶對(duì)文件具有讀,寫,執(zhí)行的權(quán)限
// S_IRUSR:用戶對(duì)文件具有讀的權(quán)限
// S_IWUSR:用戶對(duì)文件具有寫的權(quán)限
// S_IXUSR:用戶對(duì)文件具有執(zhí)行的權(quán)限
// S_IRWXG:用戶對(duì)文件具有讀,寫,執(zhí)行的權(quán)限
// S_IRGRP:用戶對(duì)文件具有讀的權(quán)限
// S_IWGRP:用戶對(duì)文件具有寫的權(quán)限
// S_IXGRP:用戶對(duì)文件具有執(zhí)行的權(quán)限
// S_IRWXO:用戶對(duì)文件具有讀,寫,執(zhí)行的權(quán)限
// S_IROTH:用戶對(duì)文件具有讀的權(quán)限
// S_IWOTH:用戶對(duì)文件具有寫的權(quán)限
// S_IXOTH:用戶對(duì)文件具有執(zhí)行的權(quán)限
//當(dāng)需要指定多個(gè)不同權(quán)限時(shí),使用“|“連接: S_IRWXU | S_IRGRP
//使用mode指定的文件權(quán)限的方式在指定的路徑下創(chuàng)建文件
//返回該文件新的文件描述符
//int creat(const char *pathname, mode_t mode);
int myOpen(const char *pathname)
{
int fd = -1;
//以只讀方式打開文件,若文件不存在,不會(huì)自動(dòng)創(chuàng)建文件,打開失敗
fd=open("stu.info",O_RDONLY);
if(-1==fd)
{
if(2==errno)
{
//以用戶具有讀寫權(quán)限,用戶組具有讀權(quán)限
//其他用戶具有讀權(quán)限的方式創(chuàng)建文件
//相當(dāng)于創(chuàng)建一個(gè)文件,并且以寫的方式打開該文件
//并且將該文件內(nèi)容截?cái)啵ㄇ蹇瘴募? fd=creat("stu.info",S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if(-1==fd)
{
printf("erron=%s\n",strerror(errno));
return 0;
}
else
{
printf("create file ok\n");
}
}
}
else
{
printf("erron=%s\n",strerror(errno));
}
return fd;
}
int main()
{
int fd=-1;
fd=myOpen("stu.info");
if(-1!=fd)
{
printf("open file ok...\n");
close(fd);
}
return 0;
}
//1_create.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>//錯(cuò)誤號(hào)erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路徑:
//絕對(duì)路徑:以/開始的路徑稱之為絕對(duì)路徑
//相對(duì)路徑:以./開始的路徑稱之為相對(duì)路徑,./可以省略不寫
//使用flag指定的方式打開指定路徑的文件
//若文件打開成功,返回該文件的一個(gè)新的文件描述符
//文件描述符用于文件操作
//若文件打開失敗,返回-1
//int open(const char *pathname,int flag);
int main()
{
int fd = -1;
//以只讀方式打開文件,若文件不存在,不會(huì)自動(dòng)創(chuàng)建文件,打開失敗
//O_CREAT | O_EXCL:
// 若文件存在,則創(chuàng)建失敗,并且打開文件失敗
// 若文件不存在,則創(chuàng)建文件,然后按照指定的打開方式打開文件
//fd=open("stu.info",O_RDONLY | O_CREAT | O_EXCL,S_IRWXU | S_IWGRP | S_IROTH);
//O_CREAT:
//若文件不存在,則按照指定的權(quán)限創(chuàng)建文件,并且打開文件。然后按照指定的打開方式打開文件
//若文件存在,則按照指定的打開方式打開文件
fd=open("stu.info",O_RDONLY | O_CREAT,S_IRWXU | S_IWGRP | S_IROTH);
if(-1==fd)
{
printf("open file failed...\n");
//當(dāng)打開失敗,會(huì)對(duì)errno設(shè)置錯(cuò)誤值
//該錯(cuò)誤值代表發(fā)生的錯(cuò)誤
printf("errno=%d\n",errno);
//將strerror獲得erron代表的錯(cuò)誤信息
printf("erron:%s\n",strerror(errno));
}
else
{
printf("open file ok...\n");
//關(guān)閉打開的文件描述符:關(guān)閉文件
close(fd);
}
return 0;
}
//write.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>//錯(cuò)誤號(hào)erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路徑:
//絕對(duì)路徑:以/開始的路徑稱之為絕對(duì)路徑
//相對(duì)路徑:以./開始的路徑稱之為相對(duì)路徑,./可以省略不寫
//使用flag指定的方式打開指定路徑的文件
//若文件打開成功,返回該文件的一個(gè)新的文件描述符
//文件描述符用于文件操作
//若文件打開失敗,返回-1
//int open(const char *pathname,int flag);
int main()
{
int fd = -1;
//以只讀方式打開文件,若文件不存在,不會(huì)自動(dòng)創(chuàng)建文件,打開失敗
//O_CREAT | O_EXCL:
// 若文件存在,則創(chuàng)建失敗,并且打開文件失敗
// 若文件不存在,則創(chuàng)建文件,然后按照指定的打開方式打開文件
//fd=open("stu.info",O_RDONLY | O_CREAT | O_EXCL,S_IRWXU | S_IWGRP | S_IROTH);
//O_CREAT:
//若文件不存在,則按照指定的權(quán)限創(chuàng)建文件,并且打開文件。然后按照指定的打開方式打開文件
//若文件存在,則按照指定的打開方式打開文件
fd=open("stu.info",O_WRONLY | O_CREAT,S_IRWXU | S_IWGRP | S_IROTH);
if(-1==fd)
{
printf("open file failed...\n");
//當(dāng)打開失敗,會(huì)對(duì)errno設(shè)置錯(cuò)誤值
//該錯(cuò)誤值代表發(fā)生的錯(cuò)誤
printf("errno=%d\n",errno);
//將strerror獲得erron代表的錯(cuò)誤信息
printf("erron:%s\n",strerror(errno));
}
else
{
printf("open file ok...\n");
int ret=-1;
char caBuf[32]="hello world";
ret=write(fd,caBuf,strlen(caBuf));
if(-1==ret)
{
printf("write erron:%s\n",strerror(errno));
}
else
{
printf("write %d bytes to file\n",ret);
}
//關(guān)閉打開的文件描述符:關(guān)閉文件
close(fd);
}
return 0;
}
- 大文件的讀寫
//write_big_data
#include <stdio.h>
#include <unistd.h> //write()
#include <errno.h> //errno
#include <string.h> //strerror()
/*open()*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PER_WRITE_BYTES 4096
#define NAME_LEN 32
int myOpen(const char *pathname)
{
int fd = -1;
if (NULL != pathname)
{
fd = open(pathname, O_WRONLY | O_CREAT
, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (-1 == fd)
{
printf("open error: %s\n", strerror(errno));
}
}
return fd;
}
int myWrite(int fd, char *pData, int iTotalSize)
{
//對(duì)形參的值進(jìn)行有效性檢查
if (-1 != fd && NULL != pData && iTotalSize > 0)
{
int i = 0;
//若數(shù)據(jù)量比較大是,write可能一次性寫不玩
//這種情況下,必須分多次循環(huán)的去寫
//剩余要寫的字節(jié)數(shù)
int iLeft = iTotalSize;
//已寫入的字節(jié)數(shù)
int iWirted = 0;
//寫數(shù)據(jù)時(shí)的返回值,出錯(cuò)返回-1,
//成功返回實(shí)際寫入的字節(jié)數(shù)
int ret = -1;
//如果剩余的數(shù)據(jù)量大于等于PER_WRITE_BYTES
//則指定該次寫入文件的數(shù)據(jù)量為PER_WRITE_BYTES
//否則指定為剩余的數(shù)據(jù)量:iLeft
if (iLeft >= PER_WRITE_BYTES)
{
ret = write(fd, pData, PER_WRITE_BYTES);
}
else
{
ret = write(fd, pData, iLeft);
}
if (-1 == ret)
{
printf("write error: %s\n", strerror(errno));
}
else
{
printf("%d, write %d bytes to file\n", ++i, ret);
//剩余數(shù)據(jù)量減去實(shí)際寫入的數(shù)據(jù)量
//得到新的剩余數(shù)據(jù)量
iLeft -= ret;
//已寫的數(shù)據(jù)量加上實(shí)際寫入的數(shù)據(jù)量
//得到新的已寫數(shù)據(jù)量
iWirted += ret;
//如果上次寫入沒有出錯(cuò)并且還有數(shù)據(jù)沒寫完
//則循環(huán)接著寫
while (ret && iLeft)
{
if (iLeft >= PER_WRITE_BYTES)
{
//指針往后偏移到未寫的數(shù)據(jù)位置
//從該位置開始將數(shù)據(jù)寫入文件
ret = write(fd, pData+iWirted
, PER_WRITE_BYTES);
}
else
{
ret = write(fd, pData+iWirted
, iLeft);
}
if (-1 != ret)
{
iLeft -= ret;
iWirted += ret;
printf("%d, write %d bytes to file\n"
, ++i, ret);
}
else
{
printf("write error: %s\n", strerror(errno));
}
}
}
}
}
int main(void)
{
int fd = -1;
fd = myOpen("test.info");
if (-1 != fd)
{
char caBuf[4096789] = {'A'};
myWrite(fd, caBuf, sizeof(caBuf));
close(fd);
}
return 0;
}
- 大文件的讀
#include <stdio.h>
#include <unistd.h> //write() read() sleep()
#include <errno.h> //errno
#include <string.h> //strerror()
/*open()*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PER_IO_BYTES 4096
int myOpen(const char *pathname)
{
int fd = -1;
if (NULL != pathname)
{
fd = open(pathname, O_RDONLY | O_CREAT
, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (-1 == fd)
{
printf("open error: %s\n", strerror(errno));
}
}
return fd;
}
#if 1
int myRead(int fd, char *pData, int iTotalSize)
{
//對(duì)形參的值進(jìn)行有效性檢查
if (-1 != fd && NULL != pData && iTotalSize > 0)
{
int i = 0;
//若要讀的數(shù)據(jù)量比較大時(shí),read可能一次性讀不完
//這種情況下,必須分多次循環(huán)的去讀
//剩余要讀的字節(jié)數(shù)
int iLeft = iTotalSize;
//已讀取的字節(jié)數(shù)
int iReaded = 0;
//讀數(shù)據(jù)時(shí)的返回值,出錯(cuò)返回-1,
//成功返回實(shí)際讀取的字節(jié)數(shù)
int ret = -1;
//如果剩余的數(shù)據(jù)量大于等于PER_IO_BYTES
//則指定該次讀取文件的數(shù)據(jù)量為PER_IO_BYTES
//否則指定為剩余的數(shù)據(jù)量:iLeft
if (iLeft >= PER_IO_BYTES)
{
ret = read(fd, pData, PER_IO_BYTES);
}
else
{
ret = read(fd, pData, iLeft);
}
if (-1 == ret)
{
printf("read error: %s\n", strerror(errno));
}
else
{
printf("%d, read %d bytes from file\n", ++i, ret);
// sleep(1);
//剩余數(shù)據(jù)量減去實(shí)際讀取的數(shù)據(jù)量
//得到新的剩余數(shù)據(jù)量
iLeft -= ret;
//已讀取的數(shù)據(jù)量加上實(shí)際讀取的數(shù)據(jù)量
//得到新的已讀取數(shù)據(jù)量
iReaded += ret;
//如果上次讀取沒有出錯(cuò)并且還有數(shù)據(jù)沒讀取完
//則循環(huán)接著讀
while (ret && iLeft)
{
if (iLeft >= PER_IO_BYTES)
{
ret = read(fd, pData+iReaded
, PER_IO_BYTES);
}
else
{
ret = read(fd, pData+iReaded
, iLeft);
}
if (-1 != ret)
{
iLeft -= ret;
iReaded += ret;
printf("%d, read %d bytes from file, left: %d, iReaded:%d\n"
, ++i, ret, iLeft, iReaded);
// sleep(1);
}
else
{
printf("write error: %s\n", strerror(errno));
}
}
}
}
}
#endif
//fd:要進(jìn)行讀操作的文件的文件描述符
//buf:存放讀的數(shù)據(jù)的空間首地址
//count:指定本次要從文件中讀取多少個(gè)字節(jié)
//read出錯(cuò)返回-1,成功返回實(shí)際讀取的字節(jié)數(shù)
//ssize_t read(int fd, void *buf, size_t count);
int main(void)
{
int fd = -1;
fd = myOpen("test.data");
if (-1 != fd)
{
char caBuf[1000100] = {'\0'};
myRead(fd, caBuf, sizeof(caBuf));
// caBuf[4096789-1] = '\0';
// printf("%s\n", caBuf);
close(fd);
}
return 0;
}
- 學(xué)生信息的讀寫
#include <stdio.h>
#include <errno.h>//errno
#include <string.h>//strerror()
#include <unistd.h>
/*open()*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
typedef struct student
{
int id;
char name[10];
char sex[5];
float score;
}student;
int main(void)
{
int fd=-1;
fd=open("stuWrite.info",O_WRONLY|O_CREAT,S_IRWXU|S_IWGRP|S_IROTH);//pathname:"./stu.info"
if(fd==-1)
{
printf("open file failed....\n");
printf("error:%s\n",strerror(errno));
}
else
{
printf("open file ok....\n");
student stu1={1,"zhangsan","boy",95.5};
int ret;
ret=write(fd,&stu1,sizeof(student));
if(ret==-1)
{
printf("write error:%s\n",strerror(errno));
}
else
{
printf("write %d bytes to file\n",ret);
}
close(fd);
}
return 0;
}
#include <stdio.h>
#include <unistd.h> //write()
#include <errno.h> //errno
#include <string.h> //strerror()
/*open()*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PER_WRITE_BYTES 4096
#define NAME_LEN 32
typedef struct Student
{
int id;
char name[10];
char sex[5];
float score;
}student;
int myOpen(const char *pathname)
{
int fd = -1;
if (NULL != pathname)
{
fd = open(pathname, O_RDONLY | O_CREAT
, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (-1 == fd)
{
printf("open error: %s\n", strerror(errno));
}
}
return fd;
}
int main(void)
{
int fd = -1;
fd = myOpen("stuWrite.info");
if (-1 != fd)
{
student stu;
memset(&stu,'\0',sizeof(student));
int ret=-1;
ret=read(fd,&stu,sizeof(student));
if(ret!=-1)
{
printf("id=%d name=%s sex=%s score=%f\n",stu.id,stu.name,stu.sex,stu.score);
}
else
{
printf("read error:%s\n",strerror(errno));
}
}
close(fd);
}
- 結(jié)構(gòu)體數(shù)組的讀寫
#include <stdio.h>
#include <unistd.h> //write()
#include <errno.h> //errno
#include <string.h> //strerror()
/*open()*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PER_WRITE_BYTES 4096
#define NAME_LEN 32
typedef struct Student
{
int iId;
char caName[NAME_LEN];
char cSex;
float fScore;
}Student;
int myOpen(const char *pathname)
{
int fd = -1;
if (NULL != pathname)
{
fd = open(pathname, O_WRONLY | O_CREAT
, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (-1 == fd)
{
printf("open error: %s\n", strerror(errno));
}
}
return fd;
}
int myWrite(int fd, char *pData, int iTotalSize)
{
//對(duì)形參的值進(jìn)行有效性檢查
if (-1 != fd && NULL != pData && iTotalSize > 0)
{
int i = 0;
//若數(shù)據(jù)量比較大是,write可能一次性寫不玩
//這種情況下,必須分多次循環(huán)的去寫
//剩余要寫的字節(jié)數(shù)
int iLeft = iTotalSize;
//已寫入的字節(jié)數(shù)
int iWirted = 0;
//寫數(shù)據(jù)時(shí)的返回值,出錯(cuò)返回-1,
//成功返回實(shí)際寫入的字節(jié)數(shù)
int ret = -1;
//如果剩余的數(shù)據(jù)量大于等于PER_WRITE_BYTES
//則指定該次寫入文件的數(shù)據(jù)量為PER_WRITE_BYTES
//否則指定為剩余的數(shù)據(jù)量:iLeft
if (iLeft >= PER_WRITE_BYTES)
{
ret = write(fd, pData, PER_WRITE_BYTES);
}
else
{
ret = write(fd, pData, iLeft);
}
if (-1 == ret)
{
printf("write error: %s\n", strerror(errno));
}
else
{
printf("%d, write %d bytes to file\n", ++i, ret);
//剩余數(shù)據(jù)量減去實(shí)際寫入的數(shù)據(jù)量
//得到新的剩余數(shù)據(jù)量
iLeft -= ret;
//已寫的數(shù)據(jù)量加上實(shí)際寫入的數(shù)據(jù)量
//得到新的已寫數(shù)據(jù)量
iWirted += ret;
//如果上次寫入沒有出錯(cuò)并且還有數(shù)據(jù)沒寫完
//則循環(huán)接著寫
while (ret && iLeft)
{
if (iLeft >= PER_WRITE_BYTES)
{
//指針往后偏移到未寫的數(shù)據(jù)位置
//從該位置開始將數(shù)據(jù)寫入文件
ret = write(fd, pData+iWirted
, PER_WRITE_BYTES);
}
else
{
ret = write(fd, pData+iWirted
, iLeft);
}
if (-1 != ret)
{
iLeft -= ret;
iWirted += ret;
printf("%d, write %d bytes to file\n"
, ++i, ret);
}
else
{
printf("write error: %s\n", strerror(errno));
}
}
}
}
}
int main(void)
{
int fd = -1;
fd = myOpen("test.info");
if (-1 != fd)
{
char caBuf[4096789] = {'A'};
myWrite(fd, caBuf, sizeof(caBuf));
close(fd);
}
return 0;
}
#include <stdio.h>
#include <unistd.h> //write()
#include <errno.h> //errno
#include <string.h> //strerror()
/*open()*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PER_READ_BYTES 4096
int myOpen(const char *pathname)
{
int fd = -1;
if (NULL != pathname)
{
fd = open(pathname, O_RDONLY | O_CREAT
, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (-1 == fd)
{
printf("open error: %s\n", strerror(errno));
}
}
return fd;
}
int myRead(int fd, char *pData, int iTotalSize)
{
//對(duì)形參的值進(jìn)行有效性檢查
if (-1 != fd && NULL != pData && iTotalSize > 0)
{
int i = 0;
//若數(shù)據(jù)量比較大是,read可能一次性讀不玩
//這種情況下,必須分多次循環(huán)的去讀
//剩余要讀的字節(jié)數(shù)
int iLeft = iTotalSize;
//已讀取的字節(jié)數(shù)
int iReaded = 0;
//讀數(shù)據(jù)時(shí)的返回值,出錯(cuò)返回-1,
//成功返回實(shí)際讀取的字節(jié)數(shù)
int ret = -1;
//如果剩余的數(shù)據(jù)量大于等于PER_WRITE_BYTES
//則指定該次讀取文件的數(shù)據(jù)量為PER_WRITE_BYTES
//否則指定為剩余的數(shù)據(jù)量:iLeft
if (iLeft >= PER_READ_BYTES)
{
ret = read(fd, pData, PER_READ_BYTES);
}
else
{
ret = read(fd, pData, iLeft);
}
if (-1 == ret)
{
printf("read error: %s\n", strerror(errno));
}
else
{
printf("%d, read %d bytes to file\n", ++i, ret);
//剩余數(shù)據(jù)量減去實(shí)際讀取的數(shù)據(jù)量
//得到新的剩余數(shù)據(jù)量
iLeft -= ret;
//已寫的數(shù)據(jù)量加上實(shí)際讀取的數(shù)據(jù)量
//得到新的已讀取數(shù)據(jù)量
iReaded += ret;
//如果上次寫入沒有出錯(cuò)并且還有數(shù)據(jù)沒讀完
//則循環(huán)接著讀
while (ret && iLeft)
{
if (iLeft >= PER_READ_BYTES)
{
//指針往后偏移到未寫的數(shù)據(jù)位置
//從該位置開始將數(shù)據(jù)寫入文件
ret = read(fd, pData+iReaded, PER_READ_BYTES);
}
else
{
ret = read(fd, pData+iReaded
, iLeft);
}
if (-1 != ret)
{
iLeft -= ret;
iReaded += ret;
printf("%d, read %d bytes to file,left :%d\n", ++i, ret,iReaded);
}
else
{
printf("read error: %s\n", strerror(errno));
}
}
}
}
}
int main(void)
{
int fd = -1;
fd = myOpen("test.data");
if (-1 != fd)
{
char caBuf[4096789] = {'A'};
myRead(fd, caBuf, sizeof(caBuf));
close(fd);
}
return 0;
}