 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Anthony Guest
|
Posted: Sun May 06, 2007 3:28 pm Post subject: string.find issue |
|
|
i have a single string parameter listing seperated by commas. I am trying to
read in the individual sections into variables. If i enter the parameter
listing in the same function manually to test it works fine, but i have a
seperate function that retrieves the parameter and it works fine and returns
the correct information and when i use the function i need to include the
following line or it doesnt work anymore:
string s =
"ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd";
its not concerned by the characters its the amount of characters that seems
to be making it work.
say when its working the output is:
"c:\testing location 1"
when i dont include the string s part it will only output:
"c:\testin"
a portion has been supplied, let me know if anyone
knows what im doing wrong, thanks in advance...
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <string>
#include "Unit1.h"
#include "Lnk.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//using namespace std;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int loc1 = 0, loc2 = 0, size = 0;
string LnkPth, LnkInfo, LnkArgs, LnkWrkDir, LnkHKey, LnkShwCmd, LnkIcnLcn,
sub1, sub2;
Lnk * Lnk;
char* e;
e = "c:\\ShortcutTest.lnk";
LnkInfo = Lnk->GetLnkInfo(e); //this function runs the iShellLink functions
to breakdown a shortcuts information.
string s =
"ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd";
loc1 = LnkInfo.find(",",loc1);
LnkPth = LnkInfo.substr(0,loc1); //this section is run multiple times to
retrieve the information but the it does fail on the first run.
} |
|
| Back to top |
|
 |
Bruce Larrabee Guest
|
Posted: Sun May 06, 2007 5:10 pm Post subject: Re: string.find issue |
|
|
Hi Anthony,
Well for one thing I'd make this change...
char* e = "c:\\ShortcutTest.lnk";
This way you've actually allocated some memory for the string in 'e'.
In my own programs I usually do this.
char ea = "the string";
chat *e = ea;
Or more to the point.
char ea[36];
char *e = ea;
As for as string 's' I assume it's a similar issue but can't tell from the code
provided. If I had to guess I'd guess that the program is finding enough
memory for "c:\testin". But that is only a guess.. Of course a String object
shouldn't need any special treatment. Need more code I think.
HTH,
Bruce |
|
| Back to top |
|
 |
Bob Gonder Guest
|
Posted: Sun May 06, 2007 6:47 pm Post subject: Re: string.find issue |
|
|
Anthony wrote:
| Quote: | when i use the function i need to include the
following line or it doesnt work anymore:
say when its working the output is:
"c:\testing location 1"
when i dont include the string s part it will only output:
"c:\testin"
|
That usually indicates a buffer overrun problem (or a non-existant
buffer). I suspect your class doesn't handle problems correctly.
| Quote: | Lnk * Lnk;
LnkInfo = Lnk->GetLnkInfo(e); //this function runs the iShellLink functions
to breakdown a shortcuts information.
|
1) Member function of non-existant object called.
2) No error checking.
| Quote: | loc1 = LnkInfo.find(",",loc1);
|
Doesn't Loc1 need to incriment before next call?
Isn't comma a valid value? Tab '\t' might be a better choice.
| Quote: | LnkPth = LnkInfo.substr(0,loc1); //this section is run multiple times to
|
Don't you want to get the substring starting at the previous Loc? |
|
| Back to top |
|
 |
Old Wolf Guest
|
Posted: Mon May 07, 2007 2:47 am Post subject: Re: string.find issue |
|
|
Bruce Larrabee <bruce_l (AT) westbrookent (DOT) com> wrote:
| Quote: | Well for one thing I'd make this change...
char* e = "c:\\ShortcutTest.lnk";
This way you've actually allocated some memory for the
string in 'e'.
|
Huh? The code you posted has exactly the same effect as the
original code, which was:
char *e;
e = "c:\\ShortcutTest.lnk";
The only difference is that your version has tidier source code!
String literals get allocated statically before the program
runs; you do not need to do anything special.
However, it is better if you write:
char const *e = "...";
to prevent inadvertant attempts to modify the string literal (which is not permitted in standard C++).
| Quote: | In my own programs I usually do this.
char ea = "the string";
chat *e = ea;
|
Well, that would cause a compiler error.
| Quote: | Or more to the point.
char ea[36];
char *e = ea;
|
'e' is quite redundant here, unless you plan to use it as
an iterator -- in which case it's better to not declare it
until it is necessary.
Also, this code is substantially different to the earlier
examples, in that in this case the string is modifiable,
and you also haven't assigned it a value. |
|
| Back to top |
|
 |
Old Wolf Guest
|
Posted: Mon May 07, 2007 2:52 am Post subject: Re: string.find issue |
|
|
"Anthony" <anfo (AT) hotmail (DOT) com> wrote:
| Quote: | #include "Lnk.h"
Lnk * Lnk;
|
Not a great idea to use the same identifier for the class name
and the object name, as now you will no longer be able to refer
to the class name!
| Quote: | char* e;
e = "c:\\ShortcutTest.lnk";
LnkInfo = Lnk->GetLnkInfo(e);
|
Here you dereference a pointer that has not been pointed to
anything -- this is an error.
| Quote: | string s =
"ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd";
loc1 = LnkInfo.find(",",loc1);
LnkPth = LnkInfo.substr(0,loc1);
|
You will need to post the code so that we can see what
Lnk->GetLnkInfo is doing. Specifically, from Lnk.h, the
definition and constructor of class Lnk, the GetLnkInfo
function, and any functions that those functions call.
Also include the code where you allocate the object that
you should be pointing the pointer 'Lnk' to. |
|
| Back to top |
|
 |
Bruce Larrabee Guest
|
Posted: Mon May 07, 2007 10:55 pm Post subject: Re: string.find issue |
|
|
Hi Wolf,
Hey man you are absolutely correct!
(which kinda sucks because I'm trying to be helpful..)
I misread his posting and that didn't help...
Thanks for taking care of that. I'll have to make
sure I read the code in these postings more carefully.
Thanks,
Bruce |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|