结构体中有那些可以做为它的成员呀。
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
请大家帮忙。
请参考一下Windows定义的一些Struct,你就会发现,最好保存字符串的长度信息。
很简单,head->lpfilename指向的是在while循环里的FileName,出了循环就被释放了,也就是说第二个MessageBox处的head->lpfilename是空悬指针
struct中可以使用CString,其实struct和class是差不多的。
你的问题主要出在sizeof中。因为sizeof(CString)不管你的字符串有多长,都等于4,建议你使用TCHAR ch[MAX_PATH]来代替。
换成下面这个试试
typedef struct PlayList
{
PlayList(){lppathname = "";lpfilename = "";}
CString lppathname;
CString lpfilename;
int num;
struct PlayList *next;
struct PlayList *pre;
}PLAYLIST,*LPPLAYLIST;
MFC的CString 有个毛病,不付初值的话不分配内存
gz
cstring 最好不要放到struct中去,他的长度不定,你的struct长度也就不定。
会引起一堆问题。
经验之谈:能用char *,就不要用CString,除非你是高手。
在第二个MesssageBox中的head->lpfilename是野指针,显示自然乱得一塌糊涂啦
楼上说的没错 我举双手赞成!!!在结构中尽量用char*哦:D