?
Solved

Text File reading

Posted on 1997-08-31
8
Medium Priority
?
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 

Author Comment

by:Ham
ID: 1343238
You never replied
0
 
LVL 1

Accepted Solution

by:
CFantin earned 40 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

Technology Partners: 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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

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