读文件:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //linux下面的头文件
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main(int avg, char* avgs[])
{
if (avg < 2)
{
printf("参数错误
");
return EXIT_FAILURE;
}
char s[] = "abc.txt";
int fd = open(avgs[1], O_RDONLY);
if (fd == -1)
{
printf("error :%s
", strerror(errno));
}
else
{
printf("fd=%d
", fd);
char buf[100];
memset(buf, 0, sizeof(buf));
//循环读取文件
while (read(fd, buf, sizeof(buf)-1)>0)
{
printf("buf:%s
", buf);
memset(buf, 0, sizeof(buf));
}
close(fd);
}
return 0;
}
写文件:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //linux下面的头文件
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main(int avg, char* avgs[])
{
if (avg < 2)
{
printf("参数错误
");
return EXIT_FAILURE;
}
int fd = open(avgs[1], O_RDWR | O_APPEND);
if (fd==-1)
{
printf("file open error : %s
", strerror(errno));
return EXIT_FAILURE;
}
else
{
printf("fd= %d
", fd);
char buf[100];
memset(buf, 0, sizeof(buf));
strcpy(buf, "RtesttesttestR");
int i = write(fd, buf, strlen(buf));
close(fd);
}
return 0;
}
获取文件详细信息:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //linux下面的头文件
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main(int avg, char* avgs[])
{
if (avg < 2)
{
printf("参数错误
");
return EXIT_FAILURE;
}
int fd = open(avgs[1], O_RDWR | O_APPEND);
if (fd==-1)
{
printf("file open error : %s
", strerror(errno));
return EXIT_FAILURE;
}
else
{
struct stat buf;
//获取文件信息
fstat(fd, &buf);
//判断文件是否为标准文件
if (S_ISREG(buf.st_mode))
{
printf("%s is a char file
", avgs[1]);
}
//判断文件是否为目录
if (S_ISDIR(buf.st_mode))
{
printf("%s is a directory
", avgs[1]);
}
//打印文件的大小
printf("%s file size is %d
",avgs[1], buf.st_size);
}
return 0;
}
c语言读文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int avg, char* avgs[])
{
if (avg < 2)
{
printf("avg error
");
return EXIT_FAILURE;
}
FILE* fp = fopen(avgs[1], "r");
if (fp == NULL)
{
printf("file open error");
return EXIT_FAILURE;
}
char buf[10];
memset(buf, 0, sizeof(buf));
size_t rc = 0;
while (1)
{
//fr返回读了几条记录数(fread第三个参数表示读多少条记录)
size_t fr = fread(buf, 1, 10, fp);
rc += fr;
if (fr == 0)
{
break;
}
printf("%s
", buf);
memset(buf, 0, sizeof(buf));
}
printf("size:%d
", rc);
return EXIT_SUCCESS;
}
c语言写文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//写文件
int main(int avg, char* avgs[])
{
if (avg < 2)
{
printf("avg error !
");
return EXIT_FAILURE;
}
FILE* fp = fopen(avgs[1], "a+");
if (fp == NULL)
{
printf("file open error!
");
return EXIT_FAILURE;
}
else
{
char buf[100];
memset(buf, 0, sizeof(buf));
strcpy(buf, "hello world
");
fwrite(buf, strlen(buf), 1, fp);
fclose(fp);
}
getchar();
}
二进制文件的读和写操作
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
typedef struct _Person {
char name[20];
int age;
}Person;
//二进制文件读操作
int main(int avg, char* avgs[])
{
if (avg < 2)
{
printf("avg count error!
");
return EXIT_SUCCESS;
}
FILE* fp = fopen(avgs[1], "r");
if (printf == NULL)
{
printf("file open error %s
", strerror(errno));
}
else
{
Person parray[10];
memset(&parray, 0, sizeof(Person));
while (fread(&parray[0],sizeof(Person),1,fp))
{
printf("age:%d,name:%s
", parray[0].age, parray[0].name);
}
fclose(fp);
}
return EXIT_SUCCESS;
}
//二进制文件写操作
int main(int avg, char* avgs[])
{
if (avg<2)
{
printf("avg count error!
");
return EXIT_SUCCESS;
}
FILE* fp = fopen(avgs[1], "w");
if (printf==NULL)
{
printf("file open error %s
", strerror(errno));
}
else
{
Person parray[10];
parray[0].age = 0;
strcpy(parray[0].name, "caoruipeng");
parray[1].age = 1;
strcpy(parray[1].name, "jiaruixin");
fwrite(&parray, sizeof(Person), 2, fp);
fclose(fp);
}
return EXIT_SUCCESS;
}
文件目录读写
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <dirent.h>
int main(int arg, char* args[])
{
DIR* dp;
struct dirent* dirp;
dp = opendir(args[1]);
if (dp == NULL)
{
printf("open dir error %s
", strerror(errno));
return 0;
}
while ((dirp = readdir(dp)) != NULL)
{
printf("name:%s
", dirp->d_name);
}
closedir(dp);
}
