在windows下,用C语言编写,inet_addr要怎么用呀?

在windows下,用C语言编写,inet_addr要怎么用呀?,第1张

in_addr_t inet_addr(const char *cp)

这个函数纳腔握返回的是网络顺序的四字节网络地圆困址。例如:

in_addr ipAddr

ipAddr.S_un.S_addr = inet_addr("127.0.0.1")

就是把字符串形式的ip地址转化为0xXXXXXXXX形式的地址格洞庆式。

这样:

#include <stdlib.h>

#include <stdio.h>

#include <errno.h>

#include <string.h>

#include <unistd.h>

#include <netdb.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <sys/types.h>

#include <arpa/inet.h>

#include <pthread.h>

#define MAXLINE 100

void *threadsend(void *vargp)

void *threadrecv(void *vargp)

int main()

{

int *clientfdp

clientfdp = (int *)malloc(sizeof(int))

*clientfdp = socket(AF_INET,SOCK_STREAM,0)

struct sockaddr_in serveraddr

struct hostent *hp

bzero((char *)&serveraddr,sizeof(serveraddr))

serveraddr.sin_family = AF_INET

serveraddr.sin_port = htons(15636)

serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1")

if(connect(*clientfdp,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) <0){

      printf("connect error\n")

      exit(1)

}

pthread_t tid1,tid2

printf("connected\n")

while(1){

pthread_create(&tid1,NULL,threadsend,clientfdp)

pthread_create(&tid2,NULL,threadrecv,clientfdp)

}

return EXIT_SUCCESS

}

void *threadsend(void * vargp)

{

//pthread_t tid2

int connfd = *((int *)vargp)

int idata

char temp[100]

while(1){

//printf("me: \n ")

fgets(temp,100,stdin)

send(connfd,temp,100,0)

printf("   友链       client send OK\n")

}

printf("client send\n")

return NULL

}

void *threadrecv(void *vargp)

{

char temp[100]

int connfd = *((int *)vargp)

while(1){

int idata = 0

idata = recv(connfd,temp,100,0)

if(idata >0){

printf("server :\n%s\n",temp)

}

}

return NULL

}

扩展资料:

注意事项

linux下编译多线程代码时,shell提示找键告蠢不到 pthread_create函数,原因是 pthread.h不是linux系统默认加载的库文件,应该使用类似如下gcc命令进行编译稿陪:

gcc echoserver.c -lpthread -o echoserver

只要注意 -lpthread参数就可以了。

1.简单服务器

//#include <winsock2.h>

//#pragma comment(lib,"WS2_32.lib")

WSADATA wsd

static UINT port=%%1

UINT Listen(LPVOID pParam)

{

SOCKET sServer,sClient

char buf[1024]

int retVal

if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)

{

return -1//失败缺轿

}

sServer=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)

if(INVALID_SOCKET==sServer)

{

WSACleanup()

return -1//创建套接字失败

}

SOCKADDR_IN addrServ

addrServ.sin_family=AF_INET

addrServ.sin_port=htons((short)pParam)

addrServ.sin_addr.s_addr=INADDR_ANY

retVal=bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN))

if(SOCKET_ERROR==retVal)

{

closesocket(sServer)

WSACleanup()

return -1//绑定套接字失败

}

retVal=listen(sServer,1)

if(SOCKET_ERROR==retVal)

{

closesocket(sServer)

WSACleanup()

return -1//开始监听失败

}

sockaddr_in addrClient

int addrClientlen=sizeof(addrClient)

sClient=accept(sServer,(sockaddr FAR*)&addrClient,&addrClientlen)

if(INVALID_SOCKET==sClient)

{

closesocket(sServer)

WSACleanup()

return -1//开始接受客户端连接失败

}

ZeroMemory(buf,sizeof(buf))

retVal=recv(sClient,buf,sizeof(buf),0)

if(SOCKET_ERROR==retVal)

{

closesocket(sServer)

closesocket(sClient)

WSACleanup()

return -1//接收数据失败

}

CString %%2(buf)

closesocket(sServer)

closesocket(sClient)

WSACleanup()

return 0

}

CWinThread *pThread=AfxBeginThread(Listen,&port)

2.简单旁升客户端

//#include <winsock2.h>

//#pragma comment(lib,"WS2_32.lib")

WSADATA wsd

SOCKET sHost

SOCKADDR_IN servAddr

char buf[1024]

int retVal

if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)

{

return -1//失败

}

sHost=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)

if(INVALID_SOCKET==sHost)

{

WSACleanup()

return -1//创建套接字失败伏启肆

}

servAddr.sin_family=AF_INET

servAddr.sin_addr.s_addr=inet_addr(%%3)

servAddr.sin_port=htons((short)%%2)

int nServAddlen=sizeof(servAddr)

retVal=connect(sHost,(LPSOCKADDR)&servAddr,sizeof(servAddr))

if(SOCKET_ERROR==retVal) {

closesocket(sHost)

WSACleanup()

return -1//连接服务器失败

}

ZeroMemory(buf,sizeof(buf))

strcpy(buf,%%3)

retVal=send(sHost,buf,sizeof(buf),0)

if(SOCKET_ERROR==retVal)

{

closesocket(sHost)

WSACleanup()

return -1//向服务器发送数据失败

}

closesocket(sHost)

WSACleanup()

3.获得本机IP

//#include <winsock2.h>

//#pragma comment(lib,"WS2_32.lib")

WSADATA wsd

if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)

{

return -1//失败

}

char szHostname[100],szHostaddress[200]

if(gethostname(szHostname,sizeof(szHostname))!=SOCKET_ERROR)

{

HOSTENT *pHostEnt=gethostbyname(szHostname)

if(pHostEnt!=NULL){

sprintf(szHostaddress,"%d.%d.%d.%d",

( pHostEnt->h_addr_list[0][0]&0x00ff ),

( pHostEnt->h_addr_list[0][1]&0x00ff ),

( pHostEnt->h_addr_list[0][2]&0x00ff ),

( pHostEnt->h_addr_list[0][3]&0x00ff ))

}

}

else

return

CString %%1(szHostaddress)

4.端对端通信

//#include <winsock2.h>

//#pragma comment(lib,"WS2_32.lib")

WSADATA wsd

SOCKET s

char buf[1024]

if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)

{

return -1//失败

}

s=socket(AF_INET,SOCK_DGRAM,0)

if(s==INVALID_SOCKET)

{

WSACleanup()

return -1//创建套接字失败

}

SOCKADDR_IN servAddr

servAddr.sin_family=AF_INET

servAddr.sin_addr.s_addr=inet_addr(%%1)

servAddr.sin_port=htons(INADDR_ANY)

if(bind(s,(SOCKADDR*)&servAddr,sizeof(SOCKADDR_IN))==SOCKET_ERROR)

{

closesocket(s)

WSACleanup()

return -1//绑定套接字失败

}

int nServAddrlen=sizeof(servAddr)

ZeroMemory(buf,sizeof(buf))

if(recvfrom(s,buf,sizeof(buf),0,(SOCKADDR*)&servAddr,&nServAddrlen)==SOCKET_ERROR)

{

closesocket(s)

WSACleanup()

return -1//接收数据失败

}

CString %%2(buf)

ZeroMemory(buf,sizeof(buf))

strcpy(buf,%%3)

SOCKADDR_IN clientAddr

clientAddr.sin_family=AF_INET

clientAddr.sin_addr.s_addr=inet_addr(%%4)

clientAddr.sin_port=htons((short)%%5)

int nClientlen=sizeof(clientAddr)

if(sendto(s,buf,sizeof(buf),0,(SOCKADDR*)&clientAddr,nClientlen)==SOCKET_ERROR)

{

closesocket(s)

WSACleanup()

return -1//向服务器发送数据失败

}

closesocket(s)

WSACleanup()

5.点对点通信

//#include <winsock2.h>

//#pragma comment(lib,"WS2_32.lib")

WSADATA wsd

SOCKADDR_IN addrServ,addrServ2

SOCKET sServer,sClient,sHost

int retVal

sockaddr_in addrClient

char buf[1024]

static UINT port=%%2

BOOL listenerRun=TRUE

UINT Listen(LPVOID pParam)

{

addrServ.sin_family=AF_INET

addrServ.sin_port=htons((UINT)pParam)

addrServ.sin_addr.s_addr=INADDR_ANY

retVal=bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN))

if(SOCKET_ERROR==retVal)

{

closesocket(sServer)

WSACleanup()

return -1//绑定套接字失败

}

retVal=listen(sServer,1)

if(SOCKET_ERROR==retVal)

{

closesocket(sServer)

WSACleanup()

return -1//开始监听失败

}

int addrClientlen=sizeof(addrClient)

sClient=accept(sServer,(sockaddr FAR*)&addrClient,&addClientlen)

if(INVALID_SOCKET==sClient)

{

closesocket(sServer)

WSACleanup()

return -1//接收客户端请求失败

}

while(listenerRun)

{

ZeroMemory(buf,sizeof(buf))

retVal=recv(sClient,buf,sizeof(buf))

if(SOCKET_ERROR==retVal)

{

closesocket(sServer)

closesocket(sClient)

WSACleanup()

return -1//接收客户端数据失败

}

CString %%4(buf)

}

}

if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)

{

return -1//失败

}

sServer=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)

if(INVALID_SOCKET==sServer)

{

WSACleanup()

return -1//创建套接字失败

}

CWinThread *pThread=AfxBeginThread(Listen,&port)

sHost=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)

if(INVALID_SOCKET==sHost)

{

WSACleanup()

return -1//创建套接字失败

}

servAddr2.sin_family=AF_INET

servAddr2.sin_addr.s_addr=inet_addr(%%1)

servAddr.sin_port=htons((short)%%3)

int nServerAddrlen=sizeof(servAddr2)

retVal=connect(sHost,(LPSOCKADDR)&servAddr2,sizeof(servAddr2))

if(SOCKET_ERROR==retVal)

{

closesocket(sHost)

WSACleanup()

return -1//连接失败

}

zeroMemory(buf,sizeof(buf))

strcpy(buf,%%5)

retVal=send(sHost,buf,sizeof(buf),0)

if(SOCKET_ERROR==retVal)

{

closesocket(sHost)

WSACleanup()

return -1//向发送数据失败

}

listenerRun=FALSE

DWORD dwExitCode

::GetExitCodeThread(pThread->m_hThread,&dwExitCode)

pThread=null

closesocket(sServer)

closesocket(sClient)

closesocket(sHost)

WSACleanup()

6.UDP对时服务器端

//#include <winsock2.h>

//#pragma comment(lib,"WS2_32.lib")

WSADATA wsd

SOCKET s

char buf[1024]

if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)

{

return -1//失败

}

s=socket(AF_INET,SOCK_DGRAM,0)

if(s==INVALID_SOCKET)

{

WSACleanup()

return -1//创建套接字失败

}

SOCKADDR_IN servAddr

servAddr.sin_family=AF_INET

servAddr.sin_addr.s_addr=inet_addr("127.0.0.1")

servAddr.sin_port=htons(5000)

if(bind(s,(SOCKADDR*)&servAddr,sizeof(SOCKADDR_IN))==SOCKET_ERROR)

{

closesocket(s)

WSACleanup()

return -1//绑定套接字失败

}

int nServAddrlen=sizeof(servAddr)

ZeroMemory(buf,sizeof(buf))

if(recvfrom(s,buf,sizeof(buf),0,(SOCKADDR*)&servAddr,&nServAddrlen)==SOCKET_ERROR)

{

closesocket(s)

WSACleanup()

return -1//接收数据失败

}

CString str(buf)

if(str=="TimeNow")

{

SOCKADDR_IN clientAddr

clientAddr.sin_family=AF_INET

clientAddr.sin_addr.s_addr=inet_addr("127.0.0.1")

clientAddr.sin_port=htons((short)2000)

int nClientlen=sizeof(clientAddr)

SYSTEMTIME systime

GetLocalTime(&systime)

if(sendto(s,(char *)&systime,sizeof(SYSTEMTIME),0,(SOCKADDR*)&clientAddr,nClientlen)==SOCKET_ERROR)

{

closesocket(s)

WSACleanup()

return -1//向服务器发送数据失败

}

}

closesocket(s)

WSACleanup()

7.UDP对时客户端

//#include <winsock2.h>

//#pragma comment(lib,"WS2_32.lib")

WSADATA wsd

SOCKET s

char buf[1024]

if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)

{

return -1//失败

}

s=socket(AF_INET,SOCK_DGRAM,0)

if(s==INVALID_SOCKET)

{

WSACleanup()

return -1//创建套接字失败

}

SOCKADDR_IN servAddr

servAddr.sin_family=AF_INET

servAddr.sin_addr.s_addr=inet_addr("127.0.0.1")

servAddr.sin_port=htons(2000)

if(bind(s,(SOCKADDR*)&servAddr,sizeof(SOCKADDR_IN))==SOCKET_ERROR)

{

closesocket(s)

WSACleanup()

return -1//绑定套接字失败

}

int nServAddrlen=sizeof(servAddr)

ZeroMemory(buf,sizeof(buf))

CString ss="TimeNow"

strcpy(buf,ss)

SOCKADDR_IN clientAddr

clientAddr.sin_family=AF_INET

clientAddr.sin_addr.s_addr=inet_addr("127.0.0.1")

clientAddr.sin_port=htons((short)5000)

int nClientlen=sizeof(clientAddr)

if(sendto(s,buf,sizeof(buf),0,(SOCKADDR*)&clientAddr,nClientlen)==SOCKET_ERROR)

{

closesocket(s)

WSACleanup()

return -1//向服务器发送数据失败

}

memset(buf,0,1024)

if(recvfrom(s,buf,sizeof(buf),0,(SOCKADDR*)&servAddr,&nServAddrlen)==SOCKET_ERROR)

{

closesocket(s)

WSACleanup()

return -1//接收数据失败

}

SYSTEMTIME systime

memcpy(&systime,buf,16)

SetLocalTime(&systime)//设置本地与服务器时间同步。

closesocket(s)

WSACleanup()

8.点对点传输文件

9.发送邮件

/*

#import <cdonts.dll>

#include "tchar.h"

#include "stdio.h"

*/

CoInitialize(NULL)

try

{

CDONTS::INewMailPtr spNewMail(__uuidof(CDONTS::NewMail))

spNewMail->From = _T("YourName")

spNewMail->To = _T("zxgdata@21cn.com")

spNewMail->Subject = _T("Testing")

spNewMail->Body = _T("Put your message here")

spNewMail->AttachFile(_variant_t(_bstr_t("C:\\tmp\\test\\mail\\mail.cpp")),_variant_t((long)DISP_E_PARAMNOTFOUND, VT_ERROR),_variant_t((long)DISP_E_PARAMNOTFOUND, VT_ERROR))

spNewMail->Send()

printf("send ok")

}

catch(_com_error &ComError)

{

printf("%s\n",ComError.Description())

}

CoUninitialize()

10.接收邮件

利用JMail组件快速构建邮件程序

http://www.vckbase.com/document/viewdoc/?id=684

http://www.vckbase.com/document/viewdoc/?id=712

11.多线程阻塞通信

12.多线程非阻塞通信

13.多线程文件断点续传

14.多线程多文件断点续传

15.截取屏幕

HBITMAP CopyScreenToBitmap(LPRECT lpRect)

//lpRect 代表选定区域

{

HDC hScrDC, hMemDC

// 屏幕和内存设备描述表

HBITMAP hBitmap, hOldBitmap

// 位图句柄

int nX, nY, nX2, nY2

// 选定区域坐标

int nWidth, nHeight

// 位图宽度和高度

int xScrn, yScrn

// 屏幕分辨率

// 确保选定区域不为空矩形

if (IsRectEmpty(lpRect))

return NULL

//为屏幕创建设备描述表

hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL)

//为屏幕设备描述表创建兼容的内存设备描述表

hMemDC = CreateCompatibleDC(hScrDC)

// 获得选定区域坐标

nX = lpRect- >left

nY = lpRect- >top

nX2 = lpRect- >right

nY2 = lpRect- >bottom

// 获得屏幕分辨率

xScrn = GetDeviceCaps(hScrDC, HORZRES)

yScrn = GetDeviceCaps(hScrDC, VERTRES)

//确保选定区域是可见的

if (nX 〈0)

nX = 0

if (nY 〈 0)

nY = 0

if (nX2 >xScrn)

nX2 = xScrn

if (nY2 >yScrn)

nY2 = yScrn

nWidth = nX2 - nX

nHeight = nY2 - nY

// 创建一个与屏幕设备描述表兼容的位图

hBitmap = CreateCompatibleBitmap

(hScrDC, nWidth, nHeight)

// 把新位图选到内存设备描述表中

hOldBitmap = SelectObject(hMemDC, hBitmap)

// 把屏幕设备描述表拷贝到内存设备描述表中

BitBlt(hMemDC, 0, 0, nWidth, nHeight,

hScrDC, nX, nY, SRCCOPY)

//得到屏幕位图的句柄

hBitmap = SelectObject(hMemDC, hOldBitmap)

//清除

DeleteDC(hScrDC)

DeleteDC(hMemDC)

// 返回位图句柄

return hBitmap

}

得到屏幕位图句柄以后,我们

可以把屏幕内容粘贴到剪贴板上.

if (OpenClipboard(hWnd))

//hWnd为程序窗口句柄

{

//清空剪贴板

EmptyClipboard()

//把屏幕内容粘贴到剪贴板上,

hBitmap 为刚才的屏幕位图句柄

SetClipboardData(CF_BITMAP, hBitmap)

//关闭剪贴板

CloseClipb

oard()

}

我们也可以把屏幕内容以位图格式存到磁盘文件上.

int SaveBitmapToFile(HBITMAP hBitmap ,

LPSTR lpFileName) //hBitmap 为刚才的屏幕位图句柄

{ //lpFileName 为位图文件名

HDC hDC

//设备描述表

int iBits

//当前显示分辨率下每个像素所占字节数

WORD wBitCount

//位图中每个像素所占字节数

//定义调色板大小, 位图中像素字节大小 ,

位图文件大小 , 写入文件字节数

DWORD dwPaletteSize=0,

dwBmBitsSize,

dwDIBSize, dwWritten

BITMAP Bitmap

//位图属性结构

BITMAPFILEHEADER bmfHdr

//位图文件头结构

BITMAPINFOHEADER bi

//位图信息头结构

LPBITMAPINFOHEADER lpbi

//指向位图信息头结构

HANDLE fh, hDib, hPal,hOldPal=NULL

//定义文件,分配内存句柄,调色板句柄

//计算位图文件每个像素所占字节数

hDC = CreateDC("DISPLAY",NULL,NULL,NULL)

iBits = GetDeviceCaps(hDC, BITSPIXEL) *

GetDeviceCaps(hDC, PLANES)

DeleteDC(hDC)

if (iBits 〈 = 1)

wBitCount = 1

else if (iBits 〈 = 4)

wBitCount = 4

else if (iBits 〈 = 8)

wBitCount = 8

else if (iBits 〈 = 24)

wBitCount = 24

//计算调色板大小

if (wBitCount 〈 = 8)

dwPaletteSize = (1 〈 〈 wBitCount) *

sizeof(RGBQUAD)

//设置位图信息头结构

GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap)

bi.biSize = sizeof(BITMAPINFOHEADER)

bi.biWidth = Bitmap.bmWidth

bi.biHeight = Bitmap.bmHeight

bi.biPlanes = 1

bi.biBitCount = wBitCount

bi.biCompression = BI_RGB

bi.biSi

zeImage = 0

bi.biXPelsPerMeter = 0

bi.biYPelsPerMeter = 0

bi.biClrUsed = 0

bi.biClrImportant = 0

dwBmBitsSize = ((Bitmap.bmWidth *

wBitCount+31)/32)* 4

*Bitmap.bmHeight

//为位图内容分配内存

hDib = GlobalAlloc(GHND,dwBmBitsSize+

dwPaletteSize+sizeof(BITMAPINFOHEADER))

lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib)

*lpbi = bi

// 处理调色板

hPal = GetStockObject(DEFAULT_PALETTE)

if (hPal)

{

hDC = GetDC(NULL)

hOldPal = SelectPalette(hDC, hPal, FALSE)

RealizePalette(hDC)

}

// 获取该调色板下新的像素值

GetDIBits(hDC, hBitmap, 0, (UINT) Bitmap.bmHeight,

(LPSTR)lpbi + sizeof(BITMAPINFOHEADER)

+dwPaletteSize,

(BITMAPINFOHEADER *)

lpbi, DIB_RGB_COLORS)

//恢复调色板

if (hOldPal)

{

SelectPalette(hDC, hOldPal, TRUE)

RealizePalette(hDC)

ReleaseDC(NULL, hDC)

}

//创建位图文件

fh = CreateFile(lpFileName, GENERIC_WRITE,

0, NULL, CREATE_ALWAYS,

FILE_ATTRIBUTE_NORMAL FILE_

FLAG_SEQUENTIAL_SCAN, NULL)

if (fh == INVALID_HANDLE_VALUE)

return FALSE

// 设置位图文件头

bmfHdr.bfType = 0x4D42// "BM"

dwDIBSize = sizeof(BITMAPFILEHEADER)

+ sizeof(BITMAPINFOHEADER)

+ dwPaletteSize + dwBmBitsSize

bmfHdr.bfSize = dwDIBSize

bmfHdr.bfReserved1 = 0

bmfHdr.bfReserved2 = 0

bmfHdr.bfOffBits = (DWORD)sizeof

(BITMAPFILEHEADER)

+ (DWORD)sizeof(BITMAPINFOHEADER)

+ dwPaletteSize

// 写入位图文件头

WriteFile(fh, (LPSTR)&bmfHdr, sizeof

(BITMAPFILEHEADER), &dwWritten, NULL)

// 写入位图文件其余内容

WriteFile(fh, (LPSTR)lpbi, dwDIBSize,

&dwWritten, NULL)

//清除

GlobalUnlock(hDib)

GlobalFree(hDib)

CloseHandle(fh)

}

16.聊天室服务器端逻辑

17.聊天室客户端逻辑

18.克隆对象

class Test

{

public:

Test(int temp)

{

p1=temp

}

Test(Test &c_t)//这里就是自定义的拷贝构造函数

{

cout<<"进入copy构造函数"<<endl

p1=c_t.p1//这句如果去掉就不能完成复制工作了,此句复制过程的核心语句

}

public:

int p1

}

void main()

{

Test a(99)

Test b=a

cout<<b.p1

cin.get()

}

//========================================

#include <iostream>

using namespace std

class Internet

{

public:

Internet(char *name,char *address)

{

cout<<"载入构造函数"<<endl

strcpy(Internet::name,name)

strcpy(Internet::address,address)

cname=new char[strlen(name)+1]

if(cname!=NULL)

{

strcpy(Internet::cname,name)

}

}

Internet(Internet &temp)

{

cout<<"载入COPY构造函数"<<endl

strcpy(Internet::name,temp.name)

strcpy(Internet::address,temp.address)

cname=new char[strlen(name)+1]//这里注意,深拷贝的体现!

if(cname!=NULL)

{

strcpy(Internet::cname,name)

}

}

~Internet()

{

cout<<"载入析构函数!"

delete[] cname

cin.get()

}

void show()

protected:

char name[20]

char address[30]

char *cname

}

void Internet::show()

{

cout<<name<<":"<<address<<cname<<endl

}

void test(Internet ts)

{

cout<<"载入test函数"<<endl

}

void main()

{

Internet a("中国软件开发实验室","www.cndev-lab.com")

Internet b = a

b.show()

test(b)

}

/*

RUMTIME_CLASS

运行时动态识别

RTTI

class base

{

virtual base* clone() = 0

}

class A: public base

{

virtual base* clone() { return new A}

}

class B: public base

{

virtual base* clone() { return new B}

}

int main()

{

base* p1 = new A

base* p2 = p1->clone()

return 0

}

*/

19.XML属性文件解析

//#include <string>

//using namespace std

char sRead[5192]

CFile mFile(_T(%%1),CFile::modeRead)

mFile.Read(sRead,5192)

if(sRead!=null)

{

string tmp

while(sRead!=null)

{

tmp.append(sRead)

mFile.Read(sRead,5192)

}

//%%2="Logs" //%%4="ID" //%%6="Content"

//%%3="Log" //%%5="Time"

//%%7 code %%8 time %%9 content

string target(%%7),globalTag("<"+%%2+">")

string propTag1("<"+%%5+">",endTag1("</"+%%5+">")

string propTag2("<"+%%6+">",endTag1("</"+%%6+">")

int offset=tmp.find_first_of(globalTag)

while(offset)

{

offset=tmp.find_first_of(globalTag)

string description

tmp.copy(description.begin(),tmp.find_first_of("\"",offset+1)-offset)

if(target.compare(description)==0)

{

string prop,prop2

offset=tmp.find_first_of(propTag1,offset)+strlen(%%5)+2

tmp.copy(prop.begin(),tmp.find_first_of(endTag1,offset)-

offset,offset)

offset=tmp.find_first_of(propTag2,offset)+strlen(%%6)+2

tmp.copy(prop2.begin(),tmp.find_first_of(endTag2,offset)-

offset,offset)

CString %%8(prop),%%9(prop2)

%%10

return 0

}

}

}

else

return -1

20.XML属性文件构造

//#include <string>

//using namespace std

char sRead[5192]

string description

CFile mFile(_T(%%1),CFile::modeRead)

mFile.Read(sRead,5192)

int no

if(sRead!=null)

{

string tmp

while(sRead!=null)

{

tmp.append(sRead)

mFile.Read(sRead,5192)

}

//%%2="Logs" //%%4="ID" //%%6="Content"

//%%3="Log" //%%5="Time"

//%%7 code %%8 time %%9 content

int offset=tmp.find_last_of("<"+%%3+" "+%%4)+strlen(%%3) +strlen(%%4)+4

tmp.copy(description.begin(),tmp.find_last_of("\"><"+%%5)- offset,offset)

bo=atoi(description.c_str())+1

mFile.Close()

tmp.insert(tmp.find_last_of("</"+%%2+">"),"<"+%%3+" "+%%

4+"=\""+itoa(no)+"\"><"+%%5+">"+%%8+"</"+%%5+"><"+%%6+">"+%%

9+"</"+%%6+">")

CFile file(_T(%%1),CFile::modeWrite)

file.Write(tmp.c_str()):

file.Flush()

file.Close()

}

else

{

CFile file(_T(%%1),CFile::modeWrite|CFile::modeCreate)

file.Write("<?xml version=\"1.0\" encoding=\"gb2312\"?><"+%%

2+"><"+%%3+" "+%%4+"=\"0\"><"+%%5+">"+%%8+"</"+%%5+"><"+%%

6+">"+%%9+"</"+%%6+"></"+%%3+"></"+%%2+">")

file.Flush()

file.Close()

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/8228437.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存