|
I suppose ext.toStdString() returns a std::string . In this case, you should try ext.toStdString().c_str() . Otherwise investigate which kind of value returns toStdString() .
|
|
|
|
|
|
Hi,
I am having a problem with the collection class CStringList
In the part below, coded in vc++6, when the program is executen and StrList.AddTail(pStr[i]) is reached, the error 'this program has performed an illigal operation..." comes.
Although while compiling, no error is shown.
Please can you tell me what is going wrong?
<br />
void Cop1::OnAdd() <br />
{<br />
int iCount = 0;<br />
int i;<br />
POSITION p;<br />
<br />
iCount = m_payeeList.GetSelCount();<br />
<br />
if (iCount <= 0) return ;<br />
<br />
int* pItems = new int [iCount];<br />
m_payeeList.GetSelItems(iCount, pItems);<br />
<br />
CStringList StrList;<br />
<br />
char *pStr[10];<br />
<br />
for (i=0;i<iCount; i++)<br />
{<br />
<br />
m_payeeList.GetText(pItems[i], pStr[i]);<br />
StrList.AddTail(pStr[i]);<br />
<br />
}<br />
}<br />
Thanks.
Tara
Fortitudine Vinsinues!
-- modified at 10:20 Tuesday 13th June, 2006
|
|
|
|
|
I think instead of
m_payeeList.GetText(pItems[i], pStr[i]);
StrList.AddTail(pStr[i]);
you should try
CString text;
m_payeeList.GetText(pItems[i], text);
StrList.AddTail(text);
Now you do not need pStr , which actually was not initialized.
Hope it helps.
|
|
|
|
|
Ah! That works. But I did initialised pStr:
char *pStr[10];
How do I get the string back form the StringList?
The code below is giving error. Is it correct?
POSITION p;<br />
CString s;<br />
s.Format("%s",StrList.GetNext(p)); <br />
AfxMessageBox(s,MB_OK);
Tara
Fortitudine Vinsinues!
|
|
|
|
|
I think you missed to initialize p . The following should work:
POSITION p = StrList.GetHeadPosition();
CString s = StrList.GetNext(p);
AfxMessageBox(s,MB_OK);
|
|
|
|
|
Yes! It works! Thanks a billion!
Tara
Fortitudine Vinsinues!
|
|
|
|
|
Can I not pass the String list, StrList to a function?
It is not working when I am passing it like usual function.
I get the error 'FillReportList' : cannot convert parameter 1 from 'class CStringList' to 'class CStringList'
FillReportList(StrList);<br />
<br />
FillReportList(CStringList list)<br />
{<br />
}<br />
Fortitudine Vinsinues!
|
|
|
|
|
If you need to read some values from the list, try this:
FillReportList(const CStringList <big>&</big> list)
{
} If you need to modify the list, try this:
FillReportList(CStringList <big>&</big> list)
{
}
|
|
|
|
|
Thanks. Please can you explain the consept to me? Why is it that when we use a CString object we don't put & while for CStringList we do? Is it because it is a list?
Fortitudine Vinsinues!
|
|
|
|
|
When you make a function like FillReportList(CStringList list) or FillReportList(int x) , the system actually tries to make a copy of passed values (on the "stack"). Within the function, you work with the copy.
Copying is possible for int and other types, but is not possible for CStringList . This is because CStringList is a class which does not have a "copy constructor".
Disabling of copy operation for CStringList was decided by the authors, since frequently CStringList can have big size, and therefore multiple copying can be problematic in your application from the memory usage viewpoint.
Therefore we cannot pass a CStringList object by making a copy. In this case, we should pass a reference to the object, using "& " in argument declaration. Now the system does not make a copy of your list, but simply passes a small reference (an address, a pointer).
|
|
|
|
|
Thanks a lot for explaining the concept so nicely. Now I understand why we pass some object this way. Earlier, I used to do tria-and-error to see what works!!
Tara
Fortitudine Vinsinues!
|
|
|
|
|
TheinstruTara wrote: But I did initialised pStr:
char *pStr[10];
Only if you had done something like:
for (i = 0; i < iCount; i++)
{
pStr[i] = new char[100];
m_payeeList.GetText(pItems[i], pStr[i]);
}
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
int iCount = 0;
int i;
iCount = m_payeelist.GetSelCount();
if (iCount <= 0) return ;
int* pItems = new int [iCount];
m_payeelist.GetSelItems(iCount, pItems);
CStringList StrList;
for (i=0;i<m_payeelist.GetSelCount();i++)
{
char *pStr=new char[10];
m_payeelist.GetText (pItems[i],pStr);
StrList.AddTail(pStr);
delete pStr;
}
See if this helps
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
-- modified at 10:38 Tuesday 13th June, 2006
|
|
|
|
|
It works this way too. I never seem to understand how to work with char correctly.
Thanks.
Fortitudine Vinsinues!
|
|
|
|
|
Hello everyone!
This is what I want:
fileName : is730enu.exe.mtx [Correct]
unexName : is730enu.exe [Adds 4 more chars at end]
partName : is730enu.exe.000 [Same as above, but with .000 ext]
How could I do that? Like, I want to remove the last 4 chars from fileName and add that to unexName, but then add the .000 extension on the 4 chars left, but it just adds it after... I don't get it, how can it add it if I have the string on a fixed size? Thanks!
<br />
char *partName, *unexName;<br />
<br />
<br />
unexName = malloc(strlen(fileName) - 4);<br />
strncpy(unexName, fileName, strlen(fileName) - 4);<br />
<br />
<br />
partName = malloc(strlen(fileName));<br />
partName = mtxSplitAddExt(unexName, part);<br />
<br />
return partName;
Lord Kixdemp
www.SulfurMidis.com
www.SulfurSoft.tk
[ftp://][http://][hotline://]tsfc.ath.cx
|
|
|
|
|
Any reason why you can't use std::string?
You have a memory leak for unexName and you have delegated responsiblity to the caller to free partName.
|
|
|
|
|
hfry wrote: Any reason why you can't use std::string?
It's regular C.
hfry wrote: You have a memory leak for unexName and you have delegated responsiblity to the caller to free partName.
So... What can I do about it? Thanks! (Sorry if I sounded n00bish, but I am after all... )
Lord Kixdemp
www.SulfurMidis.com
www.SulfurSoft.tk
[ftp://][http://][hotline://]tsfc.ath.cx
-- modified at 10:26 Tuesday 13th June, 2006
|
|
|
|
|
Your problem:
a) strlen returns the length of the string WITHOUT null-termination.
leads to
b) you are allocating only enough memory for the "text", not for the terminator.
and
c) strncpy realizes that there is not enough room for the null-terminator (remember, strncopy copies AT MOST n characters - including the null terminator. If you had used strcpy, you would instead have written outside your allocated memory.
Solution: use (strlen(input)+1) for mallocing and strncpy.
Cheers,
Sebastian
Note: Before someone shouts "but the mtx extension is only 3 characters", let me add: there is a dot in there as well.
--
Contra vim mortem non est medicamen in hortem.
-- modified at 10:45 Tuesday 13th June, 2006
|
|
|
|
|
If there is a malloc, there must be a free. And you shouldn't return a pointer to a C string that you allocated on the heap as it's not usually obvious to the person using your function that it's a string that he must free.
I'd probably do something like this..
int getpartname(char *filename, char *partname, size_t partnamesize)
{
int dot = -1;
int filenamelen = strlen(filename);
int pos;
for(pos = filenamelen; pos >= 0; pos--)
{
if(filename[pos] == '.')
{
dot = pos;
break;
}
}
if(dot == -1) return -1;
if(partname == 0 || partnamesize < dot + 4 + 1)
{
if(partnamesize != 0) partname[0] = '\0';
return dot + 4 + 1;
}
strncpy(partname, filename, dot);
partname[dot] = '\0';
strcat(partname, ".000");
return 0;
}
The person using your function would have 2 options
char partname[80];
getpartname("is730enu.exe.mtx", partname, sizeof(partname));
char *partname;
int len = getpartname("is730enu.exe.mtx", 0, 0);
partname = (char*) malloc(len);
getpartname("is730enu.exe.mtx", partname, len);
: :
free(partname);
Oh, and just a friendly warning about strncpy...
http://blogs.msdn.com/oldnewthing/archive/2005/01/07/348437.aspx[^]
|
|
|
|
|
You should be able to use _splitpath and _makepath for this (not sure how it will handle the double extensions, but it should be okay).
Alternatively, you can copy the string to a new string and use strtok to parse the string and then reinsert the '.' where necessary.
For performance, if you don't need the middle name for anything, you could just replace the original characters with your new ones.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: not sure how it will handle the double extensions...
Everything after the rightmost dot is considered the extension. All else, including additional dots, is considered the base name.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
That is what I thought, but I hadn't tried it with those types of filenames.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
<br />
strncpy(unexName,filename,strlen(filename)-4);<br />
unexName[strlen(filename)-4]='\0';<br />
strcpy(partName,unexName);<br />
strcat(partName,".000");<br />
printf("\n%s",partName);<br />
Try this and tell me if it worked.
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
|
|
|
|
|
Crap... I'm not getting this at all... Doesn't free ing make the string NULL ? How can I return/pass a null string? The only reason I'm using malloc() is to make the char* long enough to contain the other one... And, if I allocate only a certain amount of memory for a string, and I try to add something that's larger than that, why does it add it anyways without crashing? C is confusing... I'd be using C# if this app didn't require to work on DOS. But, hfry, why parse the file myself if the extension is always 3 chars? (plus the dot) Thanks you all!
And: VuNic, that code crashed Winders.
[edit] A few minutes later...
Now I really AM confused. If I only copy 8 characters, look at what I get:
is730enucrosoft é
...crosoft? Why is Microsoft involved here?! I got no "Microsoft" string in my whole freaking program!
But if I copy 9:
is730en
I have never been so confused in my life...
[/edit]
Lord Kixdemp
www.SulfurMidis.com
www.SulfurSoft.tk
[ftp://][http://][hotline://]tsfc.ath.cx
-- modified at 13:49 Tuesday 13th June, 2006
|
|
|
|