[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Basic Data Manipulation

Posted on 2004-11-15
8
Medium Priority
?
600 Views
Last Modified: 2013-11-17
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)
0
Comment
Question by:d-signet
  • 4
  • 3
8 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 12585525
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
 
LVL 1

Expert Comment

by:sabrE7
ID: 12591094
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
 
LVL 1

Author Comment

by:d-signet
ID: 12592605
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 13

Accepted Solution

by:
SteH earned 200 total points
ID: 12592860
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
 
LVL 1

Author Comment

by:d-signet
ID: 12602422
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
 
LVL 13

Expert Comment

by:SteH
ID: 12602453
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
 
LVL 13

Expert Comment

by:SteH
ID: 12602506
I think I found the error. Try

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

instead.
0
 
LVL 1

Author Comment

by:d-signet
ID: 12602553
You absolute star !!!!!!

Have a banana!

Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question