我要成为嵌入式高手之2月18日Linux高编第三天!!
—————————————————————————————
内容回顾
标准IO
完成文件拷贝
用fgetc和fputc
#include<stdio.h>
int main(void)
{
FILE *fsrc = NULL;
FILE *fdst = NULL;
char ch = 0;
fsrc = fopen("src.txt","r");
if (fsrc == NULL)
{
perror("fail to fopen");
return -1;
}
fdst = fopen("dst.txt","w");
if (fdst == NULL)
{
perror("fail to fopen");
return -1;
}
while (1)
{
ch = fgetc(fsrc);//成功返回字符的ASCII码值,失败返回EOF
if (ch == EOF)
{
break;
}
fputc(ch, fdst);
}
fclose(fsrc);
fclose(fdst);
return 0;
}
用fgets和fputs
while (1)
{
pret = fgets(tmpbuff, sizeof(tmpbuff), fsrc);
if (pret == NULL)
{
break;
}
fputs(tmpbuff, fdst);
}
用fread和fwrite
返回值只有两个值0、1(可能会有一部分没有读到)
返回值是从0~4096不等(更加灵活)
#include<stdio.h>
int main(void)
{
FILE *fp1 = NULL;
FILE *fp2 = NULL;
size_t ret;
char s[4096] = {0};
fp1 = fopen("des.jpg","w");
fp2 = fopen("src.jpg","r");
if (fp1 == NULL || fp2 == NULL)
{
perror("fail to fopen!");
return -1;
}
while(1)
{
ret = fread(s, 1, sizeof(s), fp2);//每次读一个字节,读s大小次
printf("ret = %ld\n",ret);
if (ret == 0)
{
break;
}
fwrite(s, 1, ret, fp1);//每次写一个字节,写ret次—可以保证最后一次写的也是读的数据个数
}
fclose(fp1);
fclose(fp2);
return 0;
}
文件IO
1、知识点
标准IO和文件IO的区别:
1、标准IO是库函数,是对系统调用的封装:
2、文件IO是系统调用,是Linux内核中的函数接口
3、标准IO是有缓存的
4、文件IO是没有缓存的
IO操作的文件:-
b:block 块设备文件(按块扫描设备信息的文件)
c:character 字符设备文件(按字符扫描设备信息的文件)
d:directory 目录文件(存放文件)
-:标准IO 普通文件(存放数据,图片,音频,压缩包,文本文件)
l:link 链接文件(快捷方式)
s:socket 套接字文件(用来套接字通信)
p:pipe 管道文件(用来进程间通信)
文件IO:
1、操作步骤:打开—读/写—关闭
2、打开文件:open
3、关闭文件:close
4、读写文件:read / write (操作二进制文件)
2、open(打开文件)
头文件:
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
功能:打开文件并且获得文件描述符(和文件一 一对应)
参数:
pathname:要打开的文件
flags:标志位
O_RDONLY 只读
O_WRONLY 只写
O_RDWR 读写
O_APPEND 追加
O_ASYNC 异步IO
O_CREAT 文件不存在创建
O_TRUNC 文件存在截断(清0)
返回值:成功返回文件描述符(很小的非负整数),失败返回-1
新生成的文件描述符总是为尚未被使用的最小的非负整数(从3开始)
0:stdin 1:stdout 2:stderr
文件IO对应的打开方式
" r " O_RDONLY
" r+ " O_RDWR
" w " O_WRONLY | O_CREAT | O_TRUNC, 0664
" w+ " O_RDWR | O_CREAT | O_TRUNC, 0664
" a " O_WRONLY | O_CREAT | O_APPEND, 0664
" a+ "` O_RDWR | O_CREAT | O_APPEND, 0664
3、close(关闭文件)
头文件:# include <unistd.h>
int close(int fd);
功能:将fd对应的文件描述符关闭
4、write / read
1)write
头文件: # include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
功能:向fd对应的文件中写入buf指向的count个字节
参数:
fd:文件描述符
buf:写入数据空间首地址
count:写入的字节数
返回值:成功返回实际写入的字节数,失败返回-1
open / close / write 使用:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
int fd = 0;
char tmpbuff[4096] = "hello world";
fd = open("a.txt",O_WRONLY | O_CREAT | O_TRUNC, 0664);
if (-1 == fd)
{
perror("fail to open");
return -1;
}
write(fd, tmpbuff, strlen(tmpbuff));
close(fd);
return 0;
}
2)read
头文件:# include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
功能:从文件描述符fd对应的文件中读取count个字节存放到buf中
参数:
fd:文件描述符
buf:存放数据空间的首地址
count:相要读取数据字节数
返回值:成功返回实际读到的字节数,失败返回-1;读到文件末尾返回0
read使用:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
int main(void)
{
int fd = 0;
ssize_t nret = 0;
char tmpbuff[4096] = {0};
fd = open("a.txt", O_RDONLY);
if (-1 == fd)
{
perror("fail to open");
return -1;
}
nret = read(fd, tmpbuff, sizeof(tmpbuff));
printf("nret = %ld\n",nret);
printf("tmpbuff = %s\n",tmpbuff);
close(fd);
return 0;
}