努力,以不负沈雅之名!
嵌入式、Linux、Cosplay、Acg、动漫、各种文学 全为我所爱矣!
交流联系:QQ88459122
本示例演示Linux下read open write 函数的使用方法。用于从filecp.txt文件复制内容到filecp1.txt。
代码如下:
//******************************************
//Linux system API progrems example
//shenya&yexing
//20150409
//******************************************
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
//upon is headfiles
int main()
{
int a=0,b=0,c=0,d=0;
char q[30];
a = open("/home/filecp.txt",O_RDWR|O_CREAT,0777); //openfilecp.txt
if(a==-1)
{
printf("open shibai!\n");
}
else
{
printf("open chenggong!\n");
printf("%d\n",a);
}
c = read(a,q,29); //reading data to value q
q[29]='\0';
if(a==-1)
{
printf("read shibai!\n");
}
else
{
printf("read chenggong!\n");
}
close(a); //close the file
b = open("/home/filecp1.txt",O_RDWR|O_CREAT,0777); //open filecp1
if(b==-1)
{
printf("open shibai!\n");
}
else
{
printf("open chenggong!\n");
printf("%d\n",b);
}
d = write(b,q,30); //writting data to filecp1.txt from volue q
if(d==-1)
{
printf("write shibai!\n");
}
else
{
printf("write chenggong!\n");
}
close(b); //close this files
return 0;
}