1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include <windows.h>
#include <iostream>
#include <sys\types.h>
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <time.h>
std::string GetLastErrorStdStr(DWORD error)
{
if (error)
{
LPVOID lpMsgBuf = 0;
DWORD bufLen = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
if (bufLen)
{
LPCSTR lpMsgStr = (LPCSTR)lpMsgBuf;
std::string result(lpMsgStr, lpMsgStr+bufLen);
LocalFree(lpMsgBuf);
return result;
}
}
return std::string("?????");
}
struct s_donnee
{
int code;
int dataSize;
bool variable;
char* data;
};
int _tmain(int argc, _TCHAR* argv[])
{
struct s_donnee don;
don.code = 5;
don.variable = true;
don.data = "coucou les petits lapin";
don.dataSize = strlen(don.data)+1;
int fd = open("test.txt", _O_RDWR | _O_CREAT, _S_IREAD | _S_IWRITE );
if (fd >= 0)
{
write(fd, &don, sizeof(don));
write(fd, don.data, don.dataSize);
close(fd);
fd = open("test.txt", _O_RDWR);
if (fd >= 0)
{
struct s_donnee ret;
read(fd, &ret, sizeof(ret));
ret.data = new char[ret.dataSize];
read(fd, ret.data, ret.dataSize);
close(fd);
std::cout << ret.data <<std::endl;
delete[] ret.data;
}
else
std::cout << "open failed" << GetLastErrorStdStr(GetLastError()).c_str() << std::endl;
}
else
std::cout << "open failed" << GetLastErrorStdStr(GetLastError()).c_str() << std::endl;
system("PAUSE");
return (0);
} |
Partager