Query

AbhiJeet
AbhiJeet used Ask the Experts™
on
Experts,
Need help.
I have an array of char eg A = array [1..100] of char;
i am putting record information from database into this array inside a loop.
eg i am getting value in the array as A = [a,b,c,d,e,f,g..........] now the problem is some time the first letter is getting duplicated, i mean the array is getting value like A=[a,a,b,c,d,e.........] this doesnot happen all time, and just first letter is getting duplicated, which is bit confusing, is any thing wrong in index, i mean shoould it be from 0 always? i mean should i use this way A= arrat[0......100] of char? will this be helpful to me? or there might be any other reason?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Hi, you don't have to start from 0 index in arrays. The problem must be in the loop where you fill the array. Please post the code...

Author

Commented:
ok to be little more clear, i am using a pointer,
A = array[1..100] of char;
TestPtr = ^A;
i am using move method to fill the array like,
move(string[1],testptr^[index],length(string))
increase(index,length(string)
Is there any problem in move method?

Author

Commented:
Ok to add few more information,
the main loop is going like this
for i = 1 to 100
begin
....................
...................
fillarray;
end;
in fillarray i am filling the array with above method.
so suppose lets say my pointer TestPtr is having value as [a,b,c,d,e,f,g,.......] then for the second counter i am not clearing this pointer, i am writing over this, so for the secod count it will be again as
[a,b,c,d,...................] like it will go on, but some time letter a is getting duplicated. i think i am clear now.
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Author

Commented:
should i clear the pointer with fillchar or something before filling?

Commented:
Hi again, one suggestion, in the fillarray add this to clear the array before filling it...

FillChar(testptr^, 255, 0);

Commented:
Hahahaha, you read my mind...

Author

Commented:
Ok, i had already tried this, but what is haapening in that case is one space is getting created like
[space,a,b,c,d,d...................] any clue?

Author

Commented:
and one more information to add is  i am writing the value from my ptr to a file, with blockwrite method.

Author

Commented:
twinsoft, are you there? dont worry i am not reading your mind. keep posting your suggestion please.

Author

Commented:
Any other delphi expert who can put some light on this?

Commented:
I think it is not a space but a empty string, something like ''. I noticed something in your code that is strange.

move(string[1],testptr^[index],length(string))

This means that you copy a string in position inside the array

I think it should be:

move(string[1],testptr^,length(string))

Author

Commented:
If you have noticed i have not to copy only one string into the array i have to copy strings 'a', 'b','c' ......................'z' into the array, so i have used index.

Commented:
Hmm, you only want to copy one char from the string right ?

Then

move(string[1],testptr^[index],length(string))

should be

move(string[1],testptr^[index],1)

Author

Commented:
This is just example, the string can be of any character. so length(string)

Commented:
Hmm,
  now i think i understood what you are doing. Your array is not exactly 100 bytes, it is more and you copy the data from strings from DB to it in a concecutive way (one field after the other for all the fields in a record) right ?

If yes,  do you seperate each field from the next with a character ?

Author

Commented:
only issue is from where the extra character is coming at starting place either a blank string or the first character of the first string,,
eg. if i clear ptr before filling using fillchar it is coming as
['','xyz','abc','ladfadfad',............................] if i dont clear the ptr then it is coming as
['XXYZ','abc','adfadf',...............................]

Commented:
Is there any chance that the data in the DB, in some records, have the first field empty or null ? Otherwise you  must post the exact code of FillArray as i see that you also add a comma between the fields, no not all code is here...

Author

Commented:
yes i use a field separator.

Commented:
Please check...

"Is there any chance that the data in the DB, in some records, have the first field empty or null ?"

The code seems ok, you need to send the hole FillArray in order to be able to help you any further...

Commented:
One more thing, try using a zero based array [0..1000] just to see what happens, it might lead us to the right direction...

Author

Commented:
Ok, i am sending the first string 'XYZ' as hard code, directly into array.
Eg say in my loop of 100 counter the array will look like
['XYZ,'ABC','ADF',.......................] for counter 1
then i will write this information into file with writeblock
then for counter2 i am writing into same array without clearing
['XYZ','adfadfs','dfasdfasdf',.........................]
and i wil write into the file with writeblock
for counter3 i am writing as
['XYZ','sasaasa',.................] and i will write into file..........................
meaning XYZ will remain same for each counter other strings will change.
And finally when i read the file i am getting XXYZ randomly in any of 100 records.
and if i clear the array i am getting '' . Please help what is messing up in between.

Commented:
Hi, a have created a function to simulate yours. I don't use a pointer, i use directly the array. It works fine like this...

procedure TForm1.Button1Click(Sender: TObject);
var
 a: array[1..100] of char;
 s: String;
 p, i: Integer;
begin
 FillChar(a, 100, 0);
 s := 'Stefanos';
 p := 1;                                        //pos in array
 move(s[1], a[p], Length(s));      //copy string's data
 inc(p, Length(s));                       //increase pos
 a[p] := ',';                                  //add comma delimiter
 inc(p, 1);                                    //increase pos
 move(s[1], a[p], Length(s));     //repeat procedure
 inc(p, Length(s));                      //increase pos
 a[p] := #0;                               //finallize string
 Memo1.Lines.Add(a);               //show it in a memo
end;
Commented:
Hi, check this code

procedure TForm1.Button1Click(Sender: TObject);
var
 a: array[1..100] of char;
begin
 FillChar(a, 100, 0);
 move('XYZ', a[1], 3);
 FillArray('stefanos', a);
 FillArray('stefanos', a);
 FillArray('stefanos', a);
 FillArray('stefanos', a);
 Memo1.Lines.Add(a);
end;

procedure TForm1.FillArray(aString: String; var aArray: Array of Char);
var
 p: Integer;
begin
 for p := Low(aArray) to High(aArray) do
  if aArray[p] = #0 then
   Break;
 aArray[p] := ',';
 inc(p);
 move(aString[1], aArray[p], Length(aString));
end;

Commented:
Small tip: use constant for array length

const
  ArrLen = 100;
var
  A: array [1..ArrLen] of Char; // array[0..ArrLen - 1]
...
  for I := 1 to ArrLen do // := 0 to ArrLen - 1 do
  ...

Author

Commented:
Partially helpful

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial