Link to home
Start Free TrialLog in
Avatar of denox
denox

asked on

manipulating string in text file using, ASAP

hi expert
i would like to ask you about manipulating string in text file.

sample of 1.txt file
03-05-2002 00:00:32,03-05-2002 00:01:26, 00054,02,02,01, 56,112223, 000, 1162281681873,00

03-05-2002 00:01:49,00-00-0000 00:00:00, 00000,02,33,01, 60,1112223,11000000024,126242125108,53

03-05-2002 00:04:06,00-00-0000 00:00:00, 00000,02,39,03, 14,0823106688,4400000003,1362823138571,53

03-05-2002 00:27:15,00-00-0000 00:00:00, 00000,02,32,05, 24,0818000001,00000000,1462818213077,53

03-05-2002 00:29:15,00-00-0000 00:00:00, 00000,02,06,00, 00,0000000000,44000001,08127738962,52

here's is my problem:
- i gotta open my 1.txt and read the 1.txt file
1.txt file has format structure like this:
date1 time1,  date2 time2, xxxxx, code incoming, code outgoing, xx, xx, xxxxxxx, xxx, xx, BNum, xx

here is the example of the 1.txt content. this file has 100.0000 row or more
date1 time1,         date2 time2,          xxxxx,
03-05-2002 00:00:32, 03-05-2002 00:01:26,  00054,

code incoming code out outgoing, xx, xx, xxxxxxx, xxx,
      02,     02,              01, 56, 1112223, 000,

    BNum,       xx  
1162281681873,  00

-convert code incoming
as u can see from the 1.txt above the all code incoming is 02 so we have to find it as code incoming format in below.
 
code incoming format
if in BNum i find code 11 (2 digit from left), code incoming wills still 02  

if in BNum i find code 12 (2 digit from left), code incoming from 02 will change into 10    

if in BNum i find code 13 (2 digit from left), code incoming from 02 will change into 08

if in BNum i find code 14 (2 digit from left), code incoming wills still 02  

if in BNum we cant find the same format like above it still remain the same like the old 1.txt

this BNum will always change and updatetable

-after  that save the new change into a new txt file
with the same structure except there is a changing in the code incoming according the 2 digit from the left that we find in BNum

result save in 2.txt
03-05-2002 00:00:32,03-05-2002 00:01:26, 00054,02,02,01, 56,112223, 000, 1162281681873,00

03-05-2002 00:01:49,00-00-0000 00:00:00, 00000,10,33,01, 60,1112223,11000000024,126242125108,53

03-05-2002 00:04:06,00-00-0000 00:00:00, 00000,08,39,03, 14,0823106688,4400000003,1362823138571,53

03-05-2002 00:27:15,00-00-0000 00:00:00, 00000,02,32,05, 24,0818000001,00000000,1462818213077,53

03-05-2002 00:29:15,00-00-0000 00:00:00, 00000,02,06,00, 00,0000000000,44000001,08127738962,52

Thanks for the attention to this email and for your answer. u will help me get rid of my trouble.
i need the answer ASAp.thank you
 
sincerely,denox
ASKER CERTIFIED SOLUTION
Avatar of Holger101497
Holger101497

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of denox
denox

ASKER

hi, Holger
can u write me the complete source code from opening and reading the 1.text file until you do the manipulation string and save it into the new.txt file. i have send u the text file it was some of the text file. it can be bigger than the file i send u in ur email address and also speed is critical and the conversion is quite often to do. please help me asap. i need help for making this program i have to finished it before tuesday. thank you for your time to read this trouble.
please try to ask some information in bf_4evr@yahoo.com if u cant find anything in the file that i've sending you.
smile Denox
Hi denox,

sorry for the delay - I guess Tuesday is over now... sorry - BUT if you need something really fast, then maybe you shouldn't wait for four days (!) before posting your answer. I've been out of town from Saturday til today...

Well, anyway:

Here's an adjusted method to transform strings. I simplified it a little bit because your file format seems to pretty fix. Some things are strange in it, but that does not matter much here.

procedure TForm1.AdjustString2(var txt1Line: string);
VAR posCodeIncoming, posBNumStart: integer;
     ch1, ch2: char;
begin
     // note: these lines are looking for the beginning of BNum
     // if the fields have fixed length, you can set the index right away! (much faster!)
     posBNumStart := Length(txt1Line)-5; //
     WHILE (txt1Line[posBNumStart-1] >= #48) AND ((txt1Line[posBNumStart-1] <= #57)) DO DEC(posBNumStart);

     posCodeIncoming := 47;
     IF (txt1Line[posBNumStart] = '1') THEN BEGIN
          IF (txt1Line[posBNumStart+1] = '2')     THEN BEGIN ch1 := '1'; ch2 := '0'; END
          ELSE IF (txt1Line[posBNumStart] = '1') THEN BEGIN ch1 := '0'; ch2 := '8'; END
          ELSE IF (txt1Line[posBNumStart] = '3') THEN BEGIN ch1 := '0'; ch2 := '2'; END
          ELSE IF (txt1Line[posBNumStart] = '4') THEN BEGIN ch1 := '0'; ch2 := '2'; END
          ELSE EXIT;
          txt1Line[posCodeIncoming] := ch1;
          txt1Line[posCodeIncoming+1] := ch2;
     END;
end;


Here's a method that converts a file:
procedure TForm1.Button2Click(Sender: TObject);
     VAR f1, f2: textfile;
     startTime: integer;
     line: string;
begin
     AssignFile(f1, Edit3.Text);
     AssignFile(f2, Edit4.Text);
     Reset(f1);
     Rewrite(f2);
     startTime := GetTickCount;
     WHILE NOT (EOF(f1)) DO BEGIN
          readln(f1, line);
          AdjustString2(line);
          writeln(f2, line);
     END;
     ShowMessage('conversion took ' + IntToStr(GetTickCount-startTime) + ' ms');
     CloseFile(f1);
     CloseFile(f2);
end;

You can simply enter any filenames you want... I had two Edit-fields on my form...
This method is VERY simple and straightforward. Some things could be done to optimize it, but is that necessary?
You said speed was important. How often do you do the conversion? Looks like you'll do it once a day?

The method above takes about 200 milliseconds on my computer. That means you could process files of 10 MB size in ONE second. Do you really need it any faster?

Let me know if you're doing OK with the code.

Good luck!
         Holger

P.S.: It's a rule at Experts-Exchange that not much information should be exchanged through e-mail because nobody else can see it...
P.P.S.: Also, you shouldn't grade an answer (or a comment!!) before you're satisfied with it or think that you won't get a better answer from that expert... I personally don't like "B"s very much ... :-)
Avatar of denox

ASKER

Hi holger thank you for your code to help me but i need a little help again:)

1. open the original CDR file
2. separated the code incoming from the program
(how to separated the code incoming formatted into a text file separate from the program? )
3. save it to a new file  
(how can i browse the CDR file in a folder and do it with no display from the form, and then save the file with changing in the code incoming. )
thank you for your help once again

program is very fast:)
Avatar of denox

ASKER

Tto Holger

still the same case but now it changing a bit.
previously we only count the position for the incoming code in the Orginical CDR and changing into the incoming code formatted that i've given to you and save it to the new.txt file.

now the code incoming formatted is written in a new file called codeincoming.txt file
codeincoming.txt file only have  formatted like below :
BNum    codeincoming
11     02
12     10
13     08
14     02
15     ..
..     ..    
..     ..
and etc // it change be change also it can be added.

after that we read the Original CDR and convert it and save it to the new.txt file

so how can we convert it from the codeincoming.txt file also compare and write the incoming code formatted into the original CDR with the same position like the original CDR (CDR03May2002.txt that i've given to you in your email at Holger@cheerful.com previously) and do the manipulation string and then save it to the new.txt file?
speed also critical. no display of the form requirement. and how to open and save the CDR file without using the open dialog or save dialog function but we can still changing the path of the folder that content our program?


thank you for your attention and also helping me with this problem.
Sincerely
Denox