嵌入式开发15天(标准IO,交叉编译,模块化编程)

来源:互联网 发布:西西影音软件下载 编辑:程序博客网 时间:2024/06/05 06:53
====================标准IO===================
fopen  fread  fwrite fseek  fclose


fopen 用打开一个文件


       #include <stdio.h>


       FILE *fopen(const char *path, const char *mode);


返回值: 成功 FILE *文件指针
失败:NULL


const char *path:文件路径
r :以只读的方式打开已存在文件
r+:以只读写的方式打开,文件必须存在。


w :以只写的文件打开,清空写入,如果不存在文件则创建
w+ :以读写的文件打开,清空写入,文件不存在则创建


a :以只写的方式打开,追加写入,文件不存在则创建。
a+:以读写的方式打开,追加写入,文件不存在则创建。




fread 读文件


       #include <stdio.h>

       size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);


返回值: 成功:返回刻度值 
失败:0
void *ptr :用来存储数据的内容地址
size_t size :每个刻度的大小
size_t nmemb    :有几个刻度
FILE *stream :文件指针


fwrite 读文件
       #include <stdio.h>


       size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);




返回值: 成功:返回实际写入的元素个数
失败:0
void *ptr :用来写入数据的内容地址
size_t size :每个刻度的大小
size_t nmemb    :有几个刻度
FILE *stream :文件指针


fclose 关闭文件
       #include <stdio.h>


       int fclose(FILE *fp);


fseek  文件偏移


       #include <stdio.h>


       int fseek(FILE *stream, long offset, int whence);


FILE *stream:文件文件指针
long offset:新位置偏移量的基准点的偏移(可正,可负,也可0)
int whence:
SEEK_SET:文件开头
SEEK_CUR:文件当前位置
SEEK_END:文件尾



用标准IO函数实现copy命令





======================目录操作======================
opendir  打开目录操作


       #include <sys/types.h>
       #include <dirent.h>


       DIR *opendir(const char *name);
返回值: 成功:指向目录流的指针
失败:NULL
const char *name:目录路径



readdir 读目录操作


       #include <dirent.h>


       struct dirent *readdir(DIR *dirp);


返回值: 成功:返回一个指向目录项的指针
失败:NULL


DIR *dirp:指向目录流的指针


           struct dirent {
               ino_t          d_ino;       /* inode number */ 文件索引号
               off_t          d_off;       /* offset to the next dirent */目录项依稀量
               unsigned short d_reclen;    /* length of this record */ 记录目录项的长度
               unsigned char  d_type;      /* type of file; not supported
                                              by all file system types */文件类型
               char           d_name[256]; /* filename */文件名
           };


closedir 目录关闭操作
       #include <sys/types.h>
       #include <dirent.h>


       int closedir(DIR *dirp);


返回值: 成功:0
失败:-1


DIR *dirp:目录流指针




练习:
在一个目录中有一些文件,有mp3文件,有格式为bmp的图片,有jpeg格式图片,还有.c文件,请将mp3格式的文件的路径存放在一个二组数组当中,把格式为bmp的图片的文件路径存放在另一个二维数组当中


strstr 在一字符串中查找指定的字符串
 while((tmp = readdir(dir))!=NULL)
{
if(strstr(tmp->d_name,"mp3")!=NULL)
{
.......//存放mp3到数组。
}
if(strstr(tmp->d_name,"bmp")!=NULL)
{
.......//存放mp3到数组。
}
}




==================交叉编译===============
命令:arm-linux-gcc


注意点:


如果出现以下情况
bash: ./hello: cannot execute binary file
检查编译时工具使用是否与平台一致


1、将编译文件上传到开发板
   ubuntu tftp 上传文件
   window tftp 上传文件
2、nfs方式挂载文件


3、用开发板的命令及SecureCRT上下传文件
rx xxx(下载文件名)  //能够去覆盖文件

rz       //不能覆盖源文件



模块化编程

http://wenku.baidu.com/link?url=BwtCLijHMkjNz0cNL0OUml0fjXxy6L8WRlPsRp0Myqh5i9G2twAXUU3T6xpkTqql0YY6q89iGVcjMJySS1BLmV2TPhCmn1rPwEEQ7vShy-m








































0 0
原创粉丝点击