1 //对应int32大小的成员 的转换 范例 2 int swapInt32(int value) 3 { 4 return ((value & 0x000000FF) << 24) | 5 ((value & 0x0000FF00) << 8) | 6 ((value & 0x00FF0000) >> 8) | 7 ((value & 0xFF000000) >> 24); 8 }
#pragma pack(push) //保存对齐状态 #pragma pack(1)//设定为1字节对齐 struct newTest{ char bParam; int aParam; }; #pragma pack(pop)//恢复对齐状态
1 class chooseStructure(ct.LittleEndianStructure):#小端结构体 2 _pack_ = 1 #字节对齐 3 _fields_ = [ 4 ("chooseFre",ct.c_float*4) 5 ]
1 int a = 10; 2 FILE *pFile = fopen("1.dat","wb"); 3 fwrite(&a,sizeof(int),1,pFile); 4 fclose(pFile);
1 newTest ntest;//为上面定义的结构体 2 pFile = fopen("1.dat", "rb"); 3 fread(&ntest,sizeof(newTest),1,pFile); 4 fclose(pFile);
1 FILE *pFile = fopen("1.txt", "w+"); 2 char buf[22] = "asdf"; 3 fwrite(buf,1,22,pFile); 4 fflush(pFile); 5 fseek(pFile,0,SEEK_SET);//一定要调整文件指针的位置 6 char ch[100]; 7 fread(ch,1,100,pFile); 8 fclose(pFile); 9 cout << ch;
1 FILE *pFile = fopen("1.txt", "r"); 2 char *pBuf; 3 fseek(pFile,0,SEEK_END); 4 //ftell文件位置指针当前位置相对于文件首的偏移字节数 5 long len = ftell(pFile); 6 pBuf = new char[len+1]; 7 rewind(pFile); 8 fread(pBuf,1,len,pFile); 9 pBuf[len] = 0; 10 fclose(pFile); 11 cout << pBuf;
1 import ctypes as ct 2 num = ct.c_int32(12) 3 f = open("1.dat","wb") 4 f.write(num) 5 f.close()
1 import struct 2 f = open("1.dat","rb") 3 s = f.read() 4 f.close() 5 i = struct.unpack('i',s) 6 print i
二、利用C++对文件读写的支持
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/119501.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...