资源描述:
《嵌c基础Linux源代码.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、ARM嵌入式Linux系统开发详解.Linux.c应用程序开发详解源代码#includeintmain(){printf("HelloWorld!");return0;}//从文件读写成块数据#includeintmain(){intbuf[1024]={0};intp;FILE*fp=fopen(“./blk_file.dat”,“rb+”);if(NULL=fp)return-1;fwrite(buf,sizeof(int),1024,fp);//把1024个数据块写入文件流fp,每个数据块4个字节/*修改b
2、uf的数据,供读取后比较*/for(i=0;i<16;i++)buf[i]=-1;p=&buf[0];//设置指针p指向buf,供从文件读取数据使用fread(p,sizeof(int),1024,fp);//从文件读取1024个数据块到buf,每个数据块4个字节/*打印从文件读取的二进制数据*/for(i=0;i<1024;i++)printf("buf[%d]=%d",i,buf[i]);fclose(fp);//最后别忘了关闭文件return0;}#includeintmain(){FILE*fp=NULL;//定义文件指针
3、char*buf[3]={//定义三个字符串,供写入文件使用"Thisisfirstline!","SecondLine!","OK,thelastline!"};chartmp_buf[3][64],*p;//定义字符串缓存,供读取文件使用inti;fp=fopen("chap7_demo.dat","rb+");//使用读写方式打开文件,并且把文件长度置0if(NULL==fp){printf("errortoopenfile!");return-1;}//把三个字符串写入文件for(i=0;i<3;i++)fputs(buf[i]
4、,fp);fseek(fp,0,SEEK_SET);//把文件指针设置到文件开头,相当于rewind(fp)//从文件读取三个字符串到缓存for(i=0;i<3;i++){p=tmp_buf[i];fgets(p,64,fp);printf("%s",p);//打印刚读取出来的字符串到屏幕}fclose(fp);//别忘记关闭文件return0;}//c_memory_test.c#include#includeintmain(){char*p_str1,*p_str2;//定义两个char*指针/*使用mallo
5、c()函数分配内存*/p_str1=(char*)malloc(32);if(NULL==p_str1){//检查内存分配是否成功printf("Allocp_str1memoryERROR!");return-1;}/*使用calloc()函数分配内存*/p_str2=(char*)calloc(32,sizeof(char));if(NULL==p_str2){//检查内存是否分配成功printf("Allocp_str2memoryERROR!");free(p_str1);//注意,这里需要释放p_str1占用的内存return-1;}
6、strcpy(p_str1,"Thisisasimplesentence.");//p_str1写入一个字符串strcpy(p_str2,p_str1);//p_str2写入与p_str1相同的字符串/*打印p_str1的结果*/printf("p_str1bymalloc():");printf("p_str1address:0x%.8x",p_str1);//p_str1的内存地址printf("p_str1:%s(%dchars)",p_str1,strlen(p_str1));//p_str1的内容/*打印p_str2的结果*/pr
7、intf("p_str2bycalloc():");printf("p_str2address:0x%.8x",p_str2);//p_str2的内存地址printf("p_str2:%s(%dchars)",p_str2,strlen(p_str2));//p_str2的内容/*为p_str1重新分配内存(减小)*/p_str1=(char*)realloc(p_str1,16);if(NULL==p_str1){//检查内存分配结果printf("Reallocp_str1memoryERROR!");free(p_str2);//
8、注意,需要释放p_str2占用的内存return-1;}p_str1[15]=' ';//写