结构体中有那些可以做为它的成员呀。
CString 好象不行耶。
LPCTSTR可以可以但是运行的时候会出问题请大家看看
我这样做的
if(dlg.DoModal() == IDOK)
{
pos = dlg.GetStartPosition();
while(pos)
{
nextnode=(LPPLAYLIST)malloc(sizeof(PLAYLIST));
CString FileName = dlg.GetNextPathName(pos);
nextnode->lppathname=FileName;//
CString PathName=dlg.GetPathName();
FileName=FileName.Right(FileName.GetLength()-PathName.GetLength()-1);
nextnode->lpfilename=FileName;//
nextnode->num=npathnum;//
m_playlist.InsertItem(npathnum,nextnode->lpfilename);
npathnum++;
if(npathnum==1)
{
head=nextnode;
MessageBox(head->lpfilename);
prenode=nextnode;
}
else
{
prenode=nownode;
nownode->next=nextnode;
}
nownode=nextnode;
if(npathnum==1)
nownode->pre=NULL;
else
nownode->pre=prenode;
}
nownode->next=NULL;
}
MessageBox(head->lpfilename);
其中链表定义为:
typedef struct PlayList
{
LPCSTR lppathname;
LPCSTR lpfilename;
int num;
struct PlayList *next;
struct PlayList *pre;
}PLAYLIST,*LPPLAYLIST;
LPPLAYLIST head=NULL,prenode=NULL,nownode=NULL,nextnode=NULL;
问题是第一个MessageBox()可以正确的输出文件名。
第二个MessageBox()就不能正确的输出文件名了,输出的全是乱码。
把LPCSTR改为LPCTSTR也不行,还不出现那样的问题。
改为CString更不行了运行时出错说什么指定的内存不能为read
请大家帮忙。
nextnode->lppathname=FileName;这一句都有不能运行。
一运行就出错。
出了循环后为什么会被释放呀。
我是用head保存这个链表呀。
那你说要保存这个链表怎么做呀。
你的问题主要出在sizeof中。因为sizeof(CString)不管你的字符串有多长,都等于4(windows 2000下),建议你使用TCHAR ch[MAX_PATH]来代替。
谢谢你的回答。
那为什么第一个MessageBox()又可以呢?
typedef struct PlayList
{
PlayList(){lppathname = "";lpfilename = "";}
CString lppathname;
CString lpfilename;
int num;
struct PlayList *next;
struct PlayList *pre;
}PLAYLIST,*LPPLAYLIST;
MFC的CString 有个毛病,不付初值的话不分配内存
会引起一堆问题。
经验之谈:能用char *,就不要用CString,除非你是高手。
楼上说的没错 我举双手赞成!!!在结构中尽量用char*哦:D