文件编程
来源:互联网 发布:南京烘焙培训班 知乎 编辑:程序博客网 时间:2024/05/20 16:36
//file_creat
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
void creat_file(char *filename)
{
if(creat(filename,0755)<0)
{
printf("creat file %sfailure!\n",filename);
exit(EXIT_FALLURE);
else
{
printf("creat file %s sucess!\n",filename);
}
}
}
int main(int argc,char *argv[])
{
int i;
if(argc<2)
{ perror("you have't input the filename ,please try again!\n");
exit(EXIT_FAILURE);
}
for(i=0;i<argc;i++)
{
creat_file(argv[i]);
}
exit(EXIT_SUCESS);
}
//file_open
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main(int argc,char *argv[i])
{
int fd;
if(argc<2)
{
printf("please input the open file pathname!\n");
exit(1);
}
if((fd=open(argv[1],O_CREAT|O_RCWR,0755))<0)
{
perror("open file failure!\n");
exit(1);
}
else
{
printf("open file %d success!\n",fd);
}
close(fd);
exit(0);
}
file_read
len=int read(int fd,-->const void *buf,size_t length)
file_write
len=int write(int fd,<--const void *buf,size_t length)
int lseek(int fd,offset_t offset(向前移动3个字节(-3),whence(SEEK_SET,SEEK_CUR,SEEK_END))
lseek(fd,0,SEEK_SET)
int access(const char *pathname,int mode(R_OK,W_OK.X_OK,F_OK))
#include<unistd.h>
int main()
{
if(access("/etc/passwd",R_OK)==0)
printf("/etc/passwd can be read!\n");
}
if((from_fd=open(argv[1],O_RDONLY))==-1)
{
fpintf(stderr,"Open %s Error:%s\n",argv[1],strerror(errno));
}
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<sys/types.h>
#include<errno.h>
#define BUFFER_SIZE 1024
int main(int argc,char **argv)
{
int from_fd,to_fd;
int bytes_read,bytes_write;
char buffer[BUFFER_SIZE];
char *ptr;
if(argc!=3)
{
fprintf(stderr,"Usage:%s fromfile tofile \n\a",argv[0]);
exit(1);
}
//打开源文件
if((from_fd=open(argv[1],0,O_RDONLY))==-1)
{
fprintf(stderr,"Open %s Error:%s\n",argv[1],strerror(errno));
exit(1);
}
//创建目标文件
if((to_fd=open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1)
{
fprintf(stderr,"Open %s Error:%s\n",argv[2],strerror(errno));
exit(1);
}
//拷贝文件的代码
while(bytes_read=read(from_fd,buffer,BUFFER_SIZE))
{
if((bytes_read==-1)&&(errno!=EINTR))
break;
else if(bytes_read>0)
{
ptr=buffer;
while(bytes_write=write(to_fd,ptr,bytes_read))
if(bytes_read==-1&&(errno!=EINTR))
break;
else if(bytes_write==bytes_read)
break;
else if(bytes_write>0)
{
ptr+=bytes_write;
bytes_read-=bytes_write;
}
}
if(bytes_write==-1)
break;
}
close(from_fd);
close(to_fd);
exit(0);
}
库函数——创建和打开
FILE *fopen(const char*filename,const char*mode)
r,rb 只读方式打开
w,wb 只写方式代开,如果文件不存在,则创建文件
a,ab 追加方式打开,如果文件不存在,则创建文件
r+,r+b,rb+ 只读方式打开
w+,w+b,wh+ 读写方式打开,如果文件不存在,则创建文件
a+,a+b,ab+ 读和追加方式打开,如果文件不存在,则创建文件
len=size_t fread(void *ptr,size_t size,(*<--)size_t n,FILE *stream)
len=size_t write(const void *ptr,size_t size,(*-->)size_t n,FILE *stream)
int fgetc(FILE *stream)
EOF==end of file
int fputc(int c,FILE *stream)
//从一个流中进行格式化输入
fscanf(FIEL *stream,char *fromat(%d,%s,%c)[,argument...])
//格式化输出到一个流中
int fprintf(FILE *stream,char *format[,argument,...])
char *getcwd(char *buffer,size_t size)
#include<unistd.h>
main()
{
char nuf[80];
getcwd(buf,sizeof(buf));
printf("current working directory %s \n",buf);
}
时间获取
#include<time.h>
time_t time(time_t *tloc)//日历时间(从1970年1月1日0点到此时经过的秒数)
//将日历时间转化成格林威治标准时间,并保存在TM结构
struct tm *gmtime(const time_t *timep)
//将日历时间转化为本地时间,并保存在TM结构
struct tm*localtime(const time_t *timep)
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
}
//将tm格式的时间转化为字符串
char *asctime(const struct tm *tm)
//将日立时间转化为本地时间的字符串
char *ctime(const time_t *timep)
#include<time.h>
#include<stdio.h>
int mian(void)
{
struct tm *ptr;
time_t lt;
lt=time(NULL);
ptr=gmtime(<);
printf(asctime(ptr));
printf(ctime(<));
return 0;
}
获取时间
获取从今日凌晨到现在的时间差
int gettimeofday(struct timeval,struct timezone *tz)
struct timeval
{
int tv_sec;//秒
int tv_usec;//微秒
}
//让函数睡眠
unsigned int sleep(unsigned int seconds)//秒
void usleep(unsigned long usec)//微秒
- 文件编程
- 文件编程
- 文件编程
- 文件编程
- 文件编程
- 文件编程
- 文件编程
- 文件编程
- 文件编程
- 文件编程
- 文件编程
- 文件编程
- 文件编程
- 文件编程
- 文件编程
- 文件编程
- 文件编程
- 文件编程
- Android ListView Item ContextMenu
- linux下的网络编程
- js常用正则表达式
- linux下的多线程
- 十个开源的Javascript框架
- 文件编程
- Ubuntu Unity快捷键
- C++中extern “C”含义深层探索
- execCommand全集
- PopupWindow
- 操作系统内存管理机制
- Nullable<T> 的实现
- 进程知识点
- Program received signal SIGPIPE, Broken pipe.