实战总结的7个C程序,好东西不私藏( 五 )

6、文件固定位置插入数据
在文件的固定位置插入固定的数据 。
#include <stdio.h>#include <stdlib.h>#include <string.h> #define BASIC_FILE_NAME  "./nandflash.bin"#define UBOOT_FILE_NAME  "./u-boot.bin"#define KERNEL_FILE_NAME "./kernel.bin"#define ROOTFS_FILE_NAME "./rootfs.bin"#define App_FILE_NAME  "./app.bin"  #define UBOOT_POSITION  0x00#define KERNEL_POSITION  0x100000#define ROOTFS_POSITION  0x500000#define APP_POSITION  0x2700000   int InsertData(FILE *pfBasic,FILE *psInsert,int s32Position){ int l_S32Ret = 0; unsigned char l_arru8Temp[1024] = {0xff};  fseek(pfBasic,s32Position,SEEK_SET); fseek(psInsert,0,SEEK_SET); while(1) {  l_S32Ret = fread(l_arru8Temp,1,1024,psInsert);  if(l_S32Ret > 0)  {   l_S32Ret = fwrite(l_arru8Temp,1,l_S32Ret,pfBasic);   if(l_S32Ret<=0)   {    printf("line %d error l_S32Ret = %d n",__LINE__,l_S32Ret);    return -1;   }  }else  {   break;  } }  return 0;}   int main(void){ int l_s32Ret = 0; FILE *l_pfBasec = NULL; FILE *l_pfUboot = NULL; FILE *l_pfKernel = NULL; FILE *l_pfRootfs = NULL; FILE *l_pfApp = NULL;   l_pfBasec = fopen(BASIC_FILE_NAME,"r+"); if(NULL==l_pfBasec) {  printf("line %d error n",__LINE__);  goto ERROR; }  l_pfUboot = fopen(UBOOT_FILE_NAME,"r"); if(NULL==l_pfUboot) {  printf("line %d error n",__LINE__);  goto ERROR; }  l_pfKernel = fopen(KERNEL_FILE_NAME,"r"); if(NULL==l_pfKernel) {  printf("line %d error n",__LINE__);  goto ERROR; }  l_pfRootfs = fopen(ROOTFS_FILE_NAME,"r"); if(NULL==l_pfRootfs) {  printf("line %d error n",__LINE__);  goto ERROR; }  l_pfApp = fopen(APP_FILE_NAME,"r"); if(NULL==l_pfApp) {  printf("line %d error n",__LINE__);  goto ERROR; }  if(0> InsertData(l_pfBasec,l_pfUboot,UBOOT_POSITION)) {  printf("line %d error n",__LINE__);  goto ERROR; }  if(0> InsertData(l_pfBasec,l_pfKernel,KERNEL_POSITION)) {  printf("line %d error n",__LINE__);  goto ERROR; }  if(0> InsertData(l_pfBasec,l_pfRootfs,ROOTFS_POSITION)) {  printf("line %d error n",__LINE__);  goto ERROR; }  if(0> InsertData(l_pfBasec,l_pfApp,APP_POSITION)) {  printf("line %d error n",__LINE__);  goto ERROR; }  ERROR: if(NULL!=l_pfBasec) {  fclose(l_pfBasec);  l_pfBasec = NULL; }  if(NULL!=l_pfUboot) {  fclose(l_pfUboot);  l_pfUboot = NULL; }  if(NULL!=l_pfKernel) {  fclose(l_pfKernel);  l_pfKernel = NULL; }   if(NULL!=l_pfRootfs) {  fclose(l_pfRootfs);  l_pfRootfs = NULL; }  if(NULL!=l_pfApp) {  fclose(l_pfApp);  l_pfApp = NULL; }  return 0;}7、获取本地IP地址
在linux设备中获取本地IP地址可以使用下面的程序 , 支持最大主机有三个网口的设备 , 当然这个网卡数可以修改 。
#include <stdio.h>#include <ifaddrs.h>#include <netinet/in.h>#include <string.h>#include <arpa/inet.h> int get_local_ip(char *ps8IpList){    struct ifaddrs *ifAddrStruct;    char l_s8IpAddr[INET_ADDRSTRLEN];    void *tmpAddrPtr;    int l_s32IPCount = 0;        getifaddrs(&ifAddrStruct);    while (ifAddrStruct != NULL)     {        if (ifAddrStruct->ifa_addr->sa_family==AF_INET)        {            tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;            inet_ntop(AF_INET, tmpAddrPtr, l_s8IpAddr, INET_ADDRSTRLEN);            if (strcmp(l_s8IpAddr, "127.0.0.1") != 0)             {                if(l_s32IPCount == 0)                {                        memcpy(ps8IpList, l_s8IpAddr, INET_ADDRSTRLEN);                } else                 {                        memcpy(ps8IpList+INET_ADDRSTRLEN, l_s8IpAddr, INET_ADDRSTRLEN);                }                l_s32IPCount++;            }        }        ifAddrStruct=ifAddrStruct->ifa_next;    }     freeifaddrs(ifAddrStruct);    return l_s32IPCount;} int main(){    char l_arrs8IpAddrList[3][INET_ADDRSTRLEN];    int l_s32AddrCount;        memset(l_arrs8IpAddrList, 0, sizeof(l_arrs8IpAddrList));     l_s32AddrCount = get_local_ip(*l_arrs8IpAddrList);     for(l_s32AddrCount;l_s32AddrCount>0;l_s32AddrCount--)    {        printf("Server Local IP%d: %sn",l_s32AddrCount,l_arrs8IpAddrList[l_s32AddrCount-1]);    }  return 0;}


推荐阅读