Solved

manipulating string in text file using, ASAP

Posted on 2002-05-08
5
268 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:denox
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
Holger101497 earned 300 total points
Comment Utility
Hi denox,

I've written a procedure that converts one line.

Because your files are very large, it will probably be best to txt1 for reading and txt2 for writing at the same time, then always read one line from txt1 and write it to txt2. Do you need any help diong that?? If so, please send me a file of about 50-100 lines to my e-mail address (Holger@cheerful.com)...

Also, before I write a file conversion, I need some additional information: Do you need to do this conversion only once or very often? Is speed really critical?
How large are your files? About 5-10 MB? That should really be no problem. My code is NOT slow - it does no conversions, it just scans the strings for the needed positions.

If speed is VERY important, some little improvements can be made to the code (e.g.: don't copy the string before changing it - update one line directly).
Also, you could work on the file directly. If you replace '02' by any TWO-DIGIT number, the length will not change and it could be very fast if you just scan the file and replace some characters.
Please let me know abuot the exact requirements...

I hope my code solves your problem. Just ask if you have any questions.

To "try" this, just create a new project, add 2 TEdit-components, one TLabel and a TButton :-))

Holger

-----------------------
function TForm1.AdjustString(const txt1Line: string): string;
VAR posCodeIncoming, posBNumStart: integer;
     len, commaCount: integer;
     BNumStart, newCode: string[2];
begin
     result := txt1Line;
     Label1.Caption := '';

     // 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] <> ',') DO DEC(posBNumStart);
     WHILE (txt1Line[posBNumStart-1] >= #48) AND ((txt1Line[posBNumStart-1] <= #57)) DO DEC(posBNumStart);
     BNumStart[0] := #2;
     BNumStart[1] := txt1Line[posBNumStart];
     BNumStart[2] := txt1Line[posBNumStart+1];

     IF (BNumStart = '12') THEN newCode := '10'
     ELSE IF (BNumStart = '13') THEN newCode := '08'
     ELSE IF (BNumStart = '11') THEN newCode := '02'
     ELSE IF (BNumStart = '14') THEN newCode := '02'
     ELSE EXIT;

     posCodeIncoming := 48;
     // note: for code incoming, I assume a fixed position (dates seem to be fixed format, number seems to be always 5-digits?)
     // if this is NOT the case, you can look for it this way (brute force and not very efficient)
     //posCodeIncoming := 5;
     //commaCount := 0;
     //WHILE (commaCount < 3) DO BEGIN IF txt1Line[posCodeIncoming]=',' THEN INC(commaCount); INC(posCodeIncoming); END;

    Label1.Caption := 'first digits were ' + BNumStart + ' => code incoming changed to ' + newCode;

     result[posCodeIncoming] := newCode[1];
     result[posCodeIncoming+1] := newCode[2];

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     Edit2.Text := AdjustString(Edit1.Text);
end;
0
 

Author Comment

by:denox
Comment Utility
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
0
 
LVL 6

Expert Comment

by:Holger101497
Comment Utility
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 ... :-)
0
 

Author Comment

by:denox
Comment Utility
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:)
0
 

Author Comment

by:denox
Comment Utility
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
 
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now