Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Text File reading

Posted on 1997-08-31
8
Medium Priority
?
217 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

598 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