Solved

Text File reading

Posted on 1997-08-31
8
210 Views
Last Modified: 2010-04-04
Hello,

Q. 1st - A want to read a text file (line by line is okay).
   2nd - I want to locate certain keywords.
   3rd - Once those keywords are located, I want to write          particular sections of that line with the keyword          to a TStringGrid.

File example:
TITLE     EXAMPLE INPUT FILE    2D   DATA
ERRORS           3.0       1.0       2.0       1.0
REDUN    

COORDINATE    4                      8669.           4674.  
COORDINATE   11 EN  Evernden          451.011        884.817
COORDINATE   12 EN  Ovens           22099.220       1416.713

DIRECTION     1    7      187 43 19.  
DIRECTION     2    1       30 21 49.        
DIRECTION     2    3      257 34 24.      
     
H DIS         1    2       538.103      
H DIS         1    5       552.968    
H DIS         1    7      1655.179  

Here I want to locate the keywords 'COO', 'DIR', and 'H DI'.
Once those keywords are located, I want the text at certain positions in that line with keyword.

ie. If the keyword is 'DIR', I would like the text in         position 12 to 15 => "   2"    
    and      17 to 20 => "   3"
             27 to 29 => "257"  etc    
    If the keyword is 'H DI', I would like the text in          position  12 to 15 => "   1"    
    and       17 to 20 => "   2"    
    and       27 to 34 => "538.103"    etc

    (whether it be blanks or what not)

12345678901234567890123456789012345678901234567890
0        1         2         3         4         5
          !    !    !    !   !  !    !
DIRECTION     2    3      257 34 24.      
                          !       !
H DIS         1    2       538.103
0
Comment
Question by:Ham
  • 5
  • 2
8 Comments
 

Expert Comment

by:Bueno
ID: 1343235
Hi Ham,
I would like to help you, but you should have to be a little bit more specific... In your case the reasonable thing to do is to split the string into tokens and put them in the TStringGrid.

Unfortunatly you gathered this problem as if it is a fixed text, and I do not know why. Please give me some more information about your problem.

-- Again, the reasonable thing to do is to split the strings, usings predefined keywords (HDIS, COO, DIR... etc) and split the words as tokens using the ' ' space delimiter.
Waiting for your answer...
Amit
0
 

Author Comment

by:Ham
ID: 1343236
Hello,
I thought my question was specific enough!
The example was there - it is possible that the formatting once sent wasnt the same as typed? This may of lead to some confusion.

What other information would you require in order to solve this problem?

However I will try and reword the question -

Q. 1st - A want to read a text file (line by line is okay).
   2nd - I want to locate certain keywords with in the text file.
   3rd - Once those keywords are located, I want to write                 particular sections of that line that has the keyword            to a TStringGrid (I want to disect the line not using          spaces, not commas or not tabs but using position).

File example:
TITLE     EXAMPLE INPUT FILE    2D   DATA
ERRORS           3.0       1.0       2.0       1.0
REDUN    

COORDINATE    4                      8669.           4674.  
COORDINATE   11 EN  Evernden          451.011        884.817
COORDINATE   12 EN  Ovens           22099.220       1416.713

DIRECTION     1    7      187 43 19.  
DIRECTION     2    1       30 21 49.        
DIRECTION     2    3      257 34 24.      
     
H DIS         1    2       538.103      
H DIS         1    5       552.968    
H DIS         1    7      1655.179  

In the above example I want to locate the keywords 'COO', 'DIR', and 'H DI'.
Once those keywords are located, I want to take sections of the remaining text on that line that contains the keyword.

ie. If the keyword is 'DIR', I would like the text from position     12 to 15 => "   2"    
    17 to 20 => "   3"
    27 to 29 => "257"  etc    
   
    If the keyword is 'H DI', I would like the text in position      12 to 15 => "   1"    
    17 to 20 => "   2"    
    27 to 34 => "538.103"    etc

    (whether the text is blanks, commas or what ever)
The numbers below (2 lines) are just for position reference:
12345678901234567890123456789012345678901234567890
0        1         2         3         4         5
The line below is a marker reference showing the positions wanted
          !    !    !    !   !  !    !
The line below is a line from the above example that contains the keyword and text that I want to extract not based on spaces, commas or tabs but wanting to use positions:
DIRECTION     2    3      257 34 24.      
The line below is a marker reference showing the positions wanted
                          !       !
The line below is a line from the above example that contains the keyword and text that I want to extract not based on spaces, commas or tabs but wanting to use positions:
H DIS         1    2       538.103

The alignment of the references (numbers and marker) if formatted properly will demostrate the area of text that I require.

Thankyou, Ham
0
 

Author Comment

by:Ham
ID: 1343237
Hello,
I thought my question was specific enough!
The example was there - it is possible that the formatting once sent wasnt the same as typed? This may of lead to some confusion.

What other information would you require in order to solve this problem?

However I will try and reword the question -

Q. 1st - A want to read a text file (line by line is okay).
   2nd - I want to locate certain keywords with in the text file.
   3rd - Once those keywords are located, I want to write                 particular sections of that line that has the keyword            to a TStringGrid (I want to disect the line not using          spaces, not commas or not tabs but using position).

File example:
TITLE     EXAMPLE INPUT FILE    2D   DATA
ERRORS           3.0       1.0       2.0       1.0
REDUN    

COORDINATE    4                      8669.           4674.  
COORDINATE   11 EN  Evernden          451.011        884.817
COORDINATE   12 EN  Ovens           22099.220       1416.713

DIRECTION     1    7      187 43 19.  
DIRECTION     2    1       30 21 49.        
DIRECTION     2    3      257 34 24.      
     
H DIS         1    2       538.103      
H DIS         1    5       552.968    
H DIS         1    7      1655.179  

In the above example I want to locate the keywords 'COO', 'DIR', and 'H DI'.
Once those keywords are located, I want to take sections of the remaining text on that line that contains the keyword.

ie. If the keyword is 'DIR', I would like the text from position     12 to 15 => "   2"    
    17 to 20 => "   3"
    27 to 29 => "257"  etc    
   
    If the keyword is 'H DI', I would like the text in position      12 to 15 => "   1"    
    17 to 20 => "   2"    
    27 to 34 => "538.103"    etc

    (whether the text is blanks, commas or what ever)
The numbers below (2 lines) are just for position reference:
12345678901234567890123456789012345678901234567890
0        1         2         3         4         5
The line below is a marker reference showing the positions wanted
          !    !    !    !   !  !    !
The line below is a line from the above example that contains the keyword and text that I want to extract not based on spaces, commas or tabs but wanting to use positions:
DIRECTION     2    3      257 34 24.      
The line below is a marker reference showing the positions wanted
                          !       !
The line below is a line from the above example that contains the keyword and text that I want to extract not based on spaces, commas or tabs but wanting to use positions:
H DIS         1    2       538.103

The alignment of the references (numbers and marker) if formatted properly will demostrate the area of text that I require.

Thankyou, Ham
0
 

Author Comment

by:Ham
ID: 1343238
You never replied
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Accepted Solution

by:
CFantin earned 20 total points
ID: 1343239
Here is some code to extract the data from the file and to check for the keywords.

I was not to clear on what the stringlist should look like though.

procedure TForm1.Button1Click(Sender: TObject);
var F: TextFile;
    SL: StringList;
    FileLine, FormatLine: String;
begin
  SL := TStringList.Create;
  AssignFile(F, 'File Name goes here with full path');
  Reset(F);
  while not EOF(F) do
  begin
    ReadLn(F, FileLine);
    if Pos('DIR', FileLine) > 0 then  //If the Keyword is DIR
    begin
      SL.Add('DIR'+
            Copy(FileLine, 12, 4) +
            Copy(FileLine, 17, 4) +
            Copy(FileLine, 27, 3);
    end
    else if Pos('H DI', FileLine) > 0 then // If the keyword is H DIR
    begin
      SL.Add('H DI' +
            Copy(FileLine, 12, 4) +
            Copy(FileLine, 17, 4) +
            Copy(FileLine, 27, 8);
    end;
  end;
end;

This will create a stringlist with all of the codes and there data,
there is no COO because you did not specify what you wanted to extract from it.
0
 

Author Comment

by:Ham
ID: 1343240
Thankyou, Ham.
0
 

Author Comment

by:Ham
ID: 1343241
Hello,
  Now I have a slight variation because the search works fine - actually too fine - it finds every reference to the keyword and does not limit itself to the (for example) first 3 columns - for instance:
12345678901234567890
DIRECTION  234  345  <--- INTERESTED IN THIS LINE (DIR in First 3                           columns).

THE DIRECTION ARE... <--- BUT NO THIS LINE (AS IT IS NOT IN THE                           FIRST 3 COLUMNS).

Or for that matter is it possible to locate the keyword in keycolumns?

Thankyou, Ham.
0
 
LVL 1

Expert Comment

by:CFantin
ID: 1343242
You can check the position of the text returned and therefor you will know where the text is in the line.

eg: (This is the check I am rewriting the ReadLn)

//LineRead is the line read from the file
//Check if the Position of 'DIR'  is at the beginning of the line
//and not anywhere in the line.
if Pos('DIR', LineRead) = 1 then
  ProcessTheLine;
 
Good luck
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

929 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

12 Experts available now in Live!

Get 1:1 Help Now