LINUX 下,编程自学一:文件操作
2014-04-24 21:06
246 查看
opendir.c
#include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<dirent.h> int main() { DIR *dir; struct dirent *ptr; struct stat filestat; dir = opendir("file"); chdir("file"); while((ptr = readdir(dir)) != NULL ) { if(strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0) continue; stat(ptr->d_name,&filestat); printf("file name :%s\t size :%d\n",ptr->d_name,filestat.st_size); } return 0; }
stat.c(文件状态)
#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
int main()
{
struct stat buf;
stat("stat.c",&buf);
printf("size = %d\n",buf.st_size);
return 0;
}
filefd.c(open,lseek等就用)
#include<stdio.h> #include<sys/types.h> #include<stdio.h> #include<unistd.h> #include<sys/stat.h> #include<fcntl.h> int main() { char buf[] = "This is test file"; int fd ; fd = open("testfile",O_CREAT|O_RDWR); perror("open"); write(fd,buf,sizeof(buf)); perror("write"); lseek(fd,100,SEEK_SET); perror("lseek"); close(fd); sleep(1); printf("\n"); fd = open("testfile",O_RDWR); perror("open"); char buf_1[128]; read(fd,buf_1,128); printf("buf_1 = %s\n",buf); close(fd); return 0; }
相关文章推荐
- linux编程之文件操作命令 linux学习心得
- linux编程之文件操作
- Linux下C编程-----IO/文件操作/内存映射 实现简单记录存储(3)
- Linux 系统调用文件编程(文件操作)
- 七、Linux系统编程-文件和IO(五)fcntl函数及常用操作、文件锁
- linux操作系统下c语言编程入门 -- (3)文件操作
- linux下c语言编程:文件操作
- Linux下C编程-----文件操作(1) 通过系统调用简单操作标准输入、标准输出、标准错误
- Linux下C编程-----IO/文件操作 模拟linux ls程序显示文件系统树形结构(2)
- Linux下C语言编程--文件的操作
- Linux学习记录--文件IO操作相关系统编程
- linux应用编程:文件操作
- linux 文件操作编程
- linux 基础编程:文件操作总结
- Linux系统编程——系统调用之 I/O 操作(文件操作)
- linux编程-文件操作(5)
- linux编程之文件操作