creat函数
#include <stdio.h>
#include <sys/types.h> //这三个creat函数要用的头文件
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h> //包含exit
#include <errno.h>
int main()
{
int ret;
ret = creat("hello.c", S_IRWXU | S_IRWXG); //返回值,文件描述符
if (-1 == ret) //创建文件失败,返回值-1,并将错误码变为相应数字
{
printf("%d\n", errno); //错误码
perror("creat");
exit(1);
}
return 0;
}
open函数和read函数
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <memory.h>
int main()
{
int fd, ret; //文件描述符 file description
char buf[] = {"helloworldhelloworld"};
#if 0
fd = open("hello.c", O_RDWR); //以读写方式打开文件
if(-1 == fd)
{
perror("open");
exit(1);
}
close(fd);
#endif
fd = open("hello.txt", O_RDWR | O_CREAT, S_IRWXU);
if (-1 == fd)
{
perror("open");
exit(1);
}
ret = write(fd, buf, sizeof(buf));
if (-1 == ret)
{
perror("write");
exit(1);
}
lseek(fd, 0, SEEK_SET);
memset(buf, 0, sizeof(buf));
ret = read(fd, buf, sizeof(buf));
if (-1 == ret)
{
perror("read");
exit(1);
}
printf("read from txt: %s\n", buf);
close(fd);
return 0;
}
使用open函数和read函数Linux命令cp
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int fp_from, fp_to, ret;
char buf[32] = {0};
if (argc != 3)
{
printf("Input Error!\n");
exit(1);
}
fp_from = open(argv[1], O_RDONLY);
if (-1 == fp_from)
{
perror("open1");
exit(1);
}
fp_to = open(argv[2], O_WRONLY | O_CREAT | O_EXCL, S_IRWXU);
if (-1 == fp_to)
{
perror("open2");
exit(1);
}
while ((ret = read(fp_from, buf, sizeof(buf) - 1)) >0)
{
ret = write(fp_to, buf, ret);
if (-1 == ret)
{
perror("write");
exit(1);
}
memset(buf, 0, sizeof(buf));
}
close(fp_from);
close(fp_to);
return 0;
}
fopen函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int ret;
FILE *fp;
char buf[32] = {0};
fp = fopen("hello.c", "a+"); //以读和追加方式打开hello.c,如果不存在则创建
if (NULL == fp)
{
perror("fopen");
exit(1);
}
ret = fseek(fp, 0, SEEK_SET);
if (-1 == ret)
{
perror("fseek");
exit(1);
}
ret = fread(buf, 1, sizeof(buf), fp);
if (-1 == ret)
{
perror("fread");
exit(1);
}
printf("read from txt : %s\n", buf);
memset(buf, 0, sizeof(buf));
scanf("%s", buf);
ret = fwrite(buf, 1, strlen(buf), fp);
if (-1 == ret)
{
perror("fwrite");
exit(1);
}
fclose(fp);
return 0;
}
获取文件的大小
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
if (argc != 2)
{
printf("Input Error!\n");
exit(1);
}
fp = fopen(argv[1], "a");
fseek(fp, 0, SEEK_END);
int length = ftell(fp);
printf("The length is %d\n", length);
return 0;
}
将结构体成员写入文件
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
struct student
{
char name[20];
int age;
char sex;
};
typedef struct student stu;
int main()
{
stu s1 = {"zhangsan", 20, 'm'};
stu s2 = {"Lisi", 30, 'f'};
int fd = open("Student.txt", O_WRONLY | O_CREAT | O_EXCL, 0666);
if (-1 == fd)
{
perror("open");
exit(1);
}
int ret = write(fd, (void *)&s1, sizeof(s1));
if (-1 == ret)
{
perror("write");
exit(1);
}
ret = write(fd, (void *)&s2, sizeof(s2));
if (-1 == ret)
{
perror("write");
exit(1);
}
stu s;
fd = open("Student.txt", O_RDONLY);
if (-1 == fd)
{
perror("open");
exit(1);
}
while (1)
{
ret = read(fd, (void *)&s, sizeof(s));
if (-1 == ret)
{
perror("read");
exit(1);
}
if (0 == ret)
{
break;
}
printf("%s %d %c\n", s.name, s.age, s.sex);
memset(&s, 0, sizeof(s));
}
return 0;
}