嵌c基础linux源代码

上传人:F****n 文档编号:99131780 上传时间:2019-09-17 格式:DOC 页数:43 大小:143.50KB
返回 下载 相关 举报
嵌c基础linux源代码_第1页
第1页 / 共43页
嵌c基础linux源代码_第2页
第2页 / 共43页
嵌c基础linux源代码_第3页
第3页 / 共43页
嵌c基础linux源代码_第4页
第4页 / 共43页
嵌c基础linux源代码_第5页
第5页 / 共43页
点击查看更多>>
资源描述

《嵌c基础linux源代码》由会员分享,可在线阅读,更多相关《嵌c基础linux源代码(43页珍藏版)》请在金锄头文库上搜索。

1、ARM嵌入式Linux系统开发详解.Linux.c应用程序开发详解源代码#include int main()printf(Hello World!n);return 0;/ 从文件读写成块数据#include int main()int buf1024 = 0;int p;FILE *fp = fopen(“./blk_file.dat”, “rb+”);if (NULL=fp)return -1;fwrite(buf, sizeof(int), 1024, fp);/ 把1024个数据块写入文件流fp,每个数据块4个字节/* 修改buf的数据,供读取后比较 */for (i=0;i16;i

2、+)bufi = -1;p = &buf0; / 设置指针p指向buf,供从文件读取数据使用fread(p, sizeof(int), 1024, fp);/ 从文件读取1024个数据块到buf,每个数据块4个字节/* 打印从文件读取的二进制数据 */for (i=0;i1024;i+)printf(buf%d = %dn, i, bufi);fclose(fp);/ 最后别忘了关闭文件return 0;#include int main()FILE *fp = NULL;/ 定义文件指针char *buf3 = / 定义三个字符串,供写入文件使用 This is first line!n,

3、Second Line!n, OK, the last line!n;char tmp_buf364, *p;/ 定义字符串缓存,供读取文件使用int i;fp = fopen(chap7_demo.dat, rb+);/ 使用读写方式打开文件,并且把文件长度置0if (NULL=fp) printf(error to open file!n); return -1;/ 把三个字符串写入文件for (i=0;i3;i+) fputs(bufi, fp);fseek(fp, 0, SEEK_SET);/ 把文件指针设置到文件开头,相当于rewind(fp)/ 从文件读取三个字符串到缓存for (

4、i=0;i3;i+) p = tmp_bufi; fgets(p, 64, fp); printf(%s, p);/ 打印刚读取出来的字符串到屏幕fclose(fp);/ 别忘记关闭文件return 0;/ c_memory_test.c#include #include int main() char *p_str1, *p_str2;/ 定义两个char*指针 /* 使用malloc()函数分配内存 */ p_str1 = (char*)malloc(32); if (NULL=p_str1) / 检查内存分配是否成功 printf(Alloc p_str1 memory ERROR!n)

5、; return -1; /* 使用calloc()函数分配内存 */ p_str2 = (char*)calloc(32, sizeof(char); if (NULL=p_str2) / 检查内存是否分配成功 printf(Alloc p_str2 memory ERROR!n); free(p_str1);/ 注意,这里需要释放p_str1占用的内存 return -1; strcpy(p_str1, This is a simple sentence.);/ p_str1写入一个字符串 strcpy(p_str2, p_str1);/ p_str2写入与p_str1相同的字符串 /*

6、打印p_str1的结果 */ printf(p_str1 by malloc():n); printf(p_str1 address: 0x%.8xn, p_str1);/ p_str1的内存地址 printf(p_str1: %s(%d chars)n, p_str1, strlen(p_str1);/ p_str1的内容 /* 打印p_str2的结果 */ printf(p_str2 by calloc():n); printf(p_str2 address: 0x%.8xn, p_str2);/ p_str2的内存地址 printf(p_str2: %s(%d chars)n, p_st

7、r2, strlen(p_str2);/ p_str2的内容 /* 为p_str1重新分配内存(减小) */ p_str1 = (char*)realloc(p_str1, 16); if (NULL=p_str1) / 检查内存分配结果 printf(Realloc p_str1 memory ERROR!n); free(p_str2);/ 注意,需要释放p_str2占用的内存 return -1; p_str115 = 0;/ 写字符串结束符 /* 为p_str2重新分配内存(增大) */ p_str2 = (char*)realloc(p_str2, 128); if (NULL=p_

8、str2) / 检查内存分配结果 printf(Realloc p_str2 memory ERROR!n); free(p_str1);/ 注意,需要释放p_str1占用的内存 return -1; strcat(p_str2, The second sentence in extra memory after realloced!); /* 打印p_str1的结果 */ printf(p_str1 after reallocedn); printf(p_str1 address: 0x%.8xn, p_str1);/ p_str1的内存地址 printf(p_str1: %s(%d cha

9、rs)n, p_str1, strlen(p_str1);/ p_str1的内容 /* 打印p_str2的结果 */ printf(p_str2 after realloced:n); printf(p_str2 address: 0x%.8xn, p_str2);/ p_str2的内存地址 printf(p_str2: %s(%d chars)n, p_str2, strlen(p_str2);/ p_str2的内容 /* 注意,最后要释放占用的内存 */ free(p_str1);/ 释放p_str1占用的内存 free(p_str2);/ 释放p_str2占用的内存 return 0;

10、/* 注意POSIX操作文件函数使用不同的头文件 */#include #include #include #include #include #include extern int errno;int main()int fd;/ 注意,文件描述符是整型值char buf64 = this is a posix file!(line1)n;off_t curr_pos;fd = open(./posix.data, O_CREAT|O_RDWR|O_EXCL, S_IRWXU); /打开一个不存在的文件,并创建文件,权限是用户可读写执行if (-1=fd) / 检查文件打开是否成功switc

11、h (errno) case EEXIST:/ 文件已存在printf(File exist!n);break;default:/ 其他错误printf(open file fail!n);break;return 0;write(fd, buf, strlen(buf);/ 把字符串写入文件curr_pos = lseek(fd, 0, SEEK_CUR);/ 取得当前文件偏移量位置printf(File Point at: %dn, curr_pos);lseek(fd, 0, SEEK_SET);/ 把文件偏移量移动到文件开头strcpy(buf, File Pointer Moved!n);write(fd, buf, strlen(buf);/ 把新的数据写入文件file_mode = fcntl(fd, F_GETFL);/ 获取文件状态标记if (-1!=file_mode) switch (file_mode&O_ACCMODE) / 检查文件状态case O_RDONLY:printf(file mode is READ ONLYn);break;case O_WRONLY:printf(file mode is WRITE ONLYn);break;case O_RDWR:printf(file mode is READ & WRITE

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 教学/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号