文件编程(Linux---C)
1、系统调用-创建
函数:int creat(const char *filename,mode_tmode)
参数:
- filename:要创建的文件名(包含路径,缺省为当前路径)
- mode:创建模式
常见创建模式:
- S_IRUSR 可读
- S_IWUSR 可写
- S_IXUSR 可执行
- S_IRWXU 可读、写、执行
除了可以使用上述宏以外,还可以直接使用数字来表示文件的访问权限:
- 可执行-> 1
- 可写-> 2
- 可读-> 4
- 上述值的和,如可写可读-> 6
- 无任何权限-> 0
实例分析(演示)
file_creat.c代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void create_file(char *filename)
{
/*创建的文件具有可读可写的属性*/
if(creat(filename,0666)<0)
{
printf("create file %s failure!\n",filename);
exit(EXIT_FAILURE);
}
else
{
printf("create file %s success!\n",filename);
}
}
int main(int argc,char *argv[])
{
/*判断入参有没有传入文件名 */
if(argc<2)
{
printf("you haven't input the filename,please try again!\n");
exit(EXIT_FAILURE);
}
create_file(argv[1]);
exit(EXIT_SUCCESS);
}
运行结果:
2、文件描述
在Linux系统中,所有打开的文件都对应一个文件描述符。文件描述符的本质是一个非负整数。当打开一个文件时,该整数由系统来分配。文件描述符的范围是0 - OPEN_MAX 。早期的UNIX版本OPEN_MAX =19,即允许每个进程同时打开20个文件,现在很多系统则将其增加至1024。
(1)系统调用-打开
- int open(const char *pathname, intflags)
- int open(const char *pathname, intflags,mode_tmode)
- pathname:要打开的文件名(包含路径,缺省为当前路径)
- flags:打开标志
- O_RDONLY 只读方式打开
- O_WRONLY 只写方式打开
- O_RDWR 读写方式打开
- O_APPEND 追加方式打开
- O_CREAT 创建一个文件
- O_NOBLOCK 非阻塞方式打开
如果使用了O_CREATE标志,则使用的函数是:
int open(const char *pathname,intflags,mode_tmode);
这时需要指定mode来表示文件的访问权限。
实例分析:file_open.c代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc,char *argv[])
{
int fd;
if(argc<2)
{
printf("please input the open file pathname!\n");
exit(1);
}
if((fd=open(argv[1],O_CREAT|O_RDWR,0755))<0)
{
perror("open file failure!\n");
exit(1);
}
else
{
printf("open file %d success!\n",fd);
}
close(fd);
exit(0);
}
编译与运行:
(2)系统调用-关闭
当我们操作完文件以后,需要关闭文件:
函数:int close(intfd)
fd: 文件描述符,来源?
(3)系统调用-读
函数:int read(int fd, const void *buf, size_tlength)
功能:
从文件描述符fd所指定的文件中读取length个字节到buf所指向的缓冲区中,返回值为实际读取的字节数。
(4)系统调用-写
函数:int write(int fd, const void *buf, size_tlength)
功能:
把length个字节从buf指向的缓冲区中写到文件描述符fd所指向的文件中,返回值为实际写入的字节数。
(5)系统调用-定位
函数:int lseek(int fd, offset_t offset, int whence)
功能:
将文件读写指针相对whence移动offset个字节。操作成功时,返回文件指针相对于文件头的位置。
whence可使用下述值:
- SEEK_SET:相对文件开头
- SEEK_CUR:相对文件读写指针的当前位置
- SEEK_END:相对文件末尾
lseek(fd, -5, SEEK_CUR)
如何利用lseek来计算文件长度?
由于lseek函数的返回值为文件指针相对于文件头的位置,因此下面调用的返回值就是文件的长度:
lseek(fd, 0, SEEK_END)
(6)系统调用-访问判断
有时我们需要判断文件是否可以进行某种操作(读,写等),这时可以使用access函数:
int access(const char*pathname,intmode)
pathname:文件名称
mode:要判断的访问权限。可以取以下值或者是他们的组合。R_OK:文件可读,W_OK:文件可写,
X_OK:文件可执行,F_OK文件存在。
返回值:当我们测试成功时,函数返回0,否则如果一个条件不符时,返回-1。
例:
#include<unistd.h>
int main()
{
if (access(“/etc/passwd”,R_OK) = =0)
printf(“/etc/passwd can be read!\n”);
}
综合实例
file_cp.c代码:
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 1024
int main(int argc,char **argv)
{
FILE *from_fd;
FILE *to_fd;
long file_len=0;
char buffer[BUFFER_SIZE];
char *ptr;
/*判断入参*/
if(argc!=3)
{
printf("Usage:%s fromfile tofile\n",argv[0]);
exit(1);
}
/* 打开源文件 */
if((from_fd=fopen(argv[1],"rb"))==NULL)
{
printf("Open %s Error\n",argv[1]);
exit(1);
}
/* 创建目的文件 */
if((to_fd=fopen(argv[2],"wb"))==NULL)
{
printf("Open %s Error\n",argv[2]);
exit(1);
}
/*测得文件大小*/
fseek(from_fd,0L,SEEK_END);
file_len=ftell(from_fd);
fseek(from_fd,0L,SEEK_SET);
printf("from file size is=%d\n",file_len);
/*进行文件拷贝*/
while(!feof(from_fd))
{
fread(buffer,BUFFER_SIZE,1,from_fd);
if(BUFFER_SIZE>=file_len)
{
fwrite(buffer,file_len,1,to_fd);
}
else
{
fwrite(buffer,BUFFER_SIZE,1,to_fd);
file_len=file_len-BUFFER_SIZE;
}
bzero(buffer,BUFFER_SIZE);
}
fclose(from_fd);
fclose(to_fd);
exit(0);
}
运行结果:
库函数-文件访问
函数:FILE *fopen(const char *filename, const char *mode)
- filename:打开的文件名(包含路径,缺省为当前路径)
- mode:打开模式
- r, rb 只读方式打开
- w, wb 只写方式打开,如果文件不存在,则创建该文件
- a, ab 追加方式打开,如果文件不存在,则创建该文件
- r+, r+b, rb+ 读写方式打开
- w+, w+b, wh+ 读写方式打开,如果文件不存在,则创建该文件
- a+, a+b, ab+ 读和追加方式打开。如果文件不存在,则创建该文件
函数:size_t fread(void *ptr, size_tsize, size_tn, FILE * stream )
功能: 从stream指向的文件中读取n个字段,每个 字段为size字节,并将读取的数据放入ptr 所指的字符数组中,返回实际已读取的字 节数。
函数: size_t fwrite (const void *ptr, size_t size, size_t n,FILE *stream)
功能: 从缓冲区ptr所指的数组中把n个字段写到 stream指向的文件中,每个字段长为size 个字节,返回实际写入的字段数。
函数:int fgetc(FILE *stream)
从指定的文件中读一个字符
#include<stdio.h>
main()
{
FILE *fp;
char ch;
if((fp=fopen("c1.txt","rt"))==NULL)
{
printf("\nCannot open file strike any key exit!");
getch();
exit(1);
}
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
fclose(fp);
}
函数:int fgetc(FILE *stream)
从指定的文件中读一个字符
#include<stdio.h>
main()
{
FILE *fp;
char ch;
if((fp=fopen("c1.txt","rt"))==NULL)
{
printf("\nCannot open file strike any key exit!");
getch();
exit(1);
}
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
fclose(fp);
}
向指定的文件中写入一个字符
#include<stdio.h>
main()
{
FILE *fp;
char ch;
if((fp=fopen("string","wt+"))==NULL)
{
printf("Cannot open file,strike any key exit!");
getch();
exit(1);
}
printf("input a string:\n");
ch=getchar();
while (ch!='\n')
{
fputc(ch,fp);
ch=getchar();
}
printf("\n");
fclose(fp);
}
函数:fscanf(FILE *stream, char *format[,argument...])
从一个流中进行格式化输入
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int i;
printf("Input an integer: ");
if (fscanf(stdin, "%d", &i))
printf("The integer read was: %i\n", i);
return 0;
}
函数:int fprintf(FILE *stream, char*format[,argument,...])
格式化输出到一个流中
#include <stdio.h>
#include <process.h>
FILE *stream;
void main( void )
{
int i = 10;
double fp = 1.5;
char s[] = "this is a string";
char c = '\n';
stream = fopen( "fprintf.out", "w" );
fprintf( stream, "%s%c", s, c );
fprintf( stream, "%d\n", i );
fprintf( stream, "%f\n", fp );
fclose( stream );
}
函数:int fseek(FILE *stream, longoffset, intwhence)
whence :
- SEEK_SET 从文件的开始处开始搜索
- SEEK_CUR 从当前位置开始搜索
- SEEK_END 从文件的结束处开始搜索
在编写程序的时候,有时候需要得到当 前路径。C库函数提供了getcwd来解决 这个问题。
函数:char *getcwd(char *buffer,size_tsize)
我们提供一个size大小的buffer,getcwd 会把当前的路径名copy 到buffer中.如果 buffer太小,函数会返回-1。
#include<unistd.h>
main()
{
char buf[80];
getcwd(buf,sizeof(buf));
printf(“current working directory : %sn”,buf);
}
#include <sys/stat.h>
int mkdir(char * dir, int mode)
功能:创建一个新目录。
返回值:0表示成功,-1表述出错。
Calendar Time:日历时间,是用“从一个标准时间点(如:1970年1月1日0点)到此时经过的秒数”来表示的时间。
#include <time.h> time_t time(time_t *tloc)功能:获取日历时间,即从1970年1月1日0点到现在所经历的秒数。
/* typedef long time_t */
struct tm *gmtime(const time_t *timep)
功能:将日历时间转化为格林威治标准时间,并保存至TM结构。struct tm *localtime(const time_t *timep)
功能:将日历时间转化为本地时间,并保存至TM结构。struct tm { int tm_sec; //秒值 int tm_min; //分钟值 int tm_hour; //小时值 int tm_mday; //本月第几日 int tm_mon; //本年第几月 int tm_year; //tm_year + 1900 = 哪一年 int tm_wday; //本周第几日 int tm_yday; //本年第几日 int tm_isdst; //日光节约时间 };
#include <time.h> #include <stdio.h> int main(void) { struct tm *local; time_t t; t=time(NULL); /*获取日历时间*/ local=localtime(&t); /*转化为本地时间*/ printf("Local hour is: %d\n",local->tm_hour); local=gmtime(&t); printf("UTC hour is: %d\n",local->tm_hour); return 0; }
char *asctime(const struct tm *tm)
功能:将tm格式的时间转化为字符串,如:Sat Jul 30 08:43:03 2005
char *ctime(const time_t *timep)
功能:将日历时间转化为本地时间的字符串形式。
#include <time.h> #include <stdio.h> int main(void) { struct tm *ptr; time_t lt; lt=time(NULL); ptr=gmtime(<); printf(asctime(ptr)); /*以本地时间的字符串方式打印*/ printf(ctime(<)); /*以本地时间的字符串方式打印*/ return 0; }
int gettimeofday(struct timeval *tv,struct timezone *tz)
功能:获取从今日凌晨到现在的时间差,常用于计
算事件耗时。
struct timeval { int tv_sec; //秒数 int tv_usec; //微妙数 };
unsigned int sleep(unsigned int seconds)
功能:使程序睡眠seconds秒。
void usleep(unsigned long usec)
功能:使程序睡眠usec微秒。