Basic Data Manipulation

hello fellow coder-monkeys

i'm using c++builder 4 (old version off a coverdisk) to read data from a remote file and grab some of it into an array

The problem I have is :

The data file has 3 fields in comma sparated format, the third field is not required. Fields 1 and 2 are numeric, field 3 (when its there) is alpha-numeric

For example :

1231231, 456456456456, hglkghjlgkhjlfhjlkfjh
1231232, 345945534534,
6326344, 435358282839,
9594939, 439588023453, fghoerhowerhoguhwergwergw
....

you get the idea


I'm only interested in field 1

I have managed to get the file imported into a memo field, and that all works fine, but i cant seem to get just the part i need.

For the moment, i'm just trying to copy the required data from 1 memo field to another, and throw the rest away, so Memo4 should contain :

1123123, 1123124, 1123125, 1123126, 1123127 ......

What i've CURRENTLY got is (in a loop):

        Memo2->SelStart = 0;
        Memo2->SelLength = 7;
        Memo2->CutToClipboard();
        Memo4->PasteFromClipboard();
        Memo2->SelStart = 0;
        Memo2->SelLength = Memo2->Lines->IndexOf("\r\n");
        Memo2->ClearSelection();

you can see what i'm TRYING to do here....get the first 7 character of a line (always the data i want), paste in into Memo4, and then grab the rest of the line and throw it.

this is the closest i've got to what i want, but i only get the same data repeated, for example

11231231123123112312311231231123123112312311231231123123 .......


it doesnt seem to be picking up the newline character. Ive tried it with "\n" and "\r" by themselves too.

I know the loop works, because my revious code just selected 7 characters and pasted those and i ended up with the entire file being pasted from one field to the other, so its definately part of the above code that needs changing.

Am i along the right lines, or barking up completely the wrong tree.

I also though of grabbing the whole line, then reading just the first 7 characters, and clearing the read line from the box, but I couldnt seem to get that working either.

I think i should also point out that i have NEVER codes in a c++ RAD tool before, and not touched c++ at ALL since the last verison 4 of standard borland c++ many many years ago.


thanks guys...i know you wont let me down......please.....dont let me down :o)
LVL 1
d-signetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteHCommented:
The easiest solution should be to read an entire line and remove the remainder. Since BCB 4.0 has problems with the STL I suggest to use the following

char Line[255];
FILE* inf = fopen ("file name", "rt"); //  read text from file.

while (!inf.eof ()) {
    fgets (Line, 255, inf);
    Line[7] = '0x00'; to terminate just after the seven digits. Or if you need to append a line feed use
    strcat (Line, "\r\n");
}
0
sabrE7Commented:
Another possible way is using the strsep function like this:

char Field[255];
char *delimeter[] = ",";
char *token;
char *myfield;

myfield = Field;   //This can be done because the Field is an array.

for(int i = 0; i  < NUM_FIELDS; i++)
     {
      token=strsep(&myfield, delimeter);
      }

--->  The token will be all of the text up to the first delimeter (which is a comma), the token data will be stored at the address of the myfield pointer, which is the array.  Once the loop is complete each element in the array will be filled with each of the separated fields, then you just chose which elements(fields) you need.  

--->  You already know how to read in the file so just add that code to the top.  You must #include <string.h>

Hope this helps
0
d-signetAuthor Commented:
thanks guys

SteH , problem is that the file is on a remote server, so i'm using a simple NMHTTP object to get the data FROM the file, rather than getting the whole file. Dont think I can use standard file reading commands to get it.

sabrE7, sorry for being stupid but i cant see how to implement this code. Remember, i'm just getting to grips with how builder works here too.


As I said, i've managed to get it all to display in a memo field, so forgetting the "file" aspect of it, i just need to read the first 7 characters of each line in this memo object, and then move to the next line.


so you're making an array and a load of pointers, myfield points to the array, and then splitting that reference to the array on the comma delimiter. So in my code, i need to get the contents of 1 line of the memo field into the Field array? how do i do that?


my current code SORT OF works, but doesnt seem to read beyond the first 7 characters of the whole memo field.



void __fastcall TForm1::Button2Click(TObject *Sender)
{
 int TotalLines = Memo2->Lines->Count;
 for (int counter = 0 ; counter < TotalLines ; counter++)
 {
        Memo2->SelStart = 0;
        Memo2->SelLength = 7;
        Memo2->CutToClipboard();
        Memo4->PasteFromClipboard();
        Memo2->SelStart = 0;
        Memo2->SelLength = Memo2->Lines->IndexOf("\r\n");
        Memo2->ClearSelection();
 }

}
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

SteHCommented:
Can't verify in detail but Lines property of a TMemo should contain single lines if indexed:

TMemo2->Lines[0]; // first line
TMemo2->Lines[1]; //

So use a for loop
for (i=0;i<TMemo2->Lines->Count;++i) { // check property for number of elements in a TStrings object.
    AnsiString asLine = TMemo2->Lines[i];  //extract one line at a time
    AnsiString asKey = asLine.SubString (1, 7);       // extract first seven chars. Could be (0, 7) as parameters. Try it out.
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
d-signetAuthor Commented:
thanks SteH

I think thats along the lines of what i'm trying to do

Ive changed my code to :

 for (int counter = 0 ; counter < TotalLines ; counter++)
 {
        AnsiString asLine = Memo2->Lines[counter];
        AnsiString asKey = asLine.SubString (1, 7);
        Memo4->Lines->Add(asKey);
}


but it flags up the firwt line saying : cannot conver 'TStrings' to 'AnsiString'

Not quite sure why though

any ideas anyone?
0
SteHCommented:
TStrings is a container class for strings. Indexing it should give a single string which can be converted to an AnsiString. IT might be that out of my had I made some mistakes on the types of properties used. Check to get the TStrings field of the memo and index that one. I will have another  short look on Google to verify my code proposition.
0
SteHCommented:
I think I found the error. Try

AnsiString asLine = Memo2->Lines->Strings[counter];

instead.
0
d-signetAuthor Commented:
You absolute star !!!!!!

Have a banana!

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.