Hooked on Text files... 
Author Message
 Hooked on Text files...

Hello!  I need help with reading text files using Turbo Pascal 6.00

Here's the text file that I'm reading from:
(test.txt)

Joe_Buster 1 433-2334 32_Blooming_Dr. 32323
Gingo_Hill 2 322-2332 12_Dingo_Ln. 21223

Here's what I got so far:

var
   A : text;
   B : char;

BEGIN
   ASSIGN(A,'test.txt');
   RESET(A);
   WHILE NOT EOF(A) DO
        BEGIN
            read(A,B);
            write(B);
            IF B = ' ' THEN
                writeln;
        END;
   readln;
END.    

The output of my program would be:

Joe_Buster
1
433-2334
32_Blooming_Dr.
32323
Gingo_Hill
2
322-2332
12_Dingo_Ln.
21223

Here's my problem:
I need to figure out a way to store each character except for the space as
a string.  I tried using the CONCAT command, but it didn't work.  I got the
part where it writes each section (Name, Number, Phone Number, etc..) on
different lines.  I can do this easily if the each data was on a separate
line in the text file,  but  I don't want to do that.

Here's the output I'm trying to get:

Name :  Joe_Buster
Number:  1
Phone Number:  433-2334
Home address:  32_Blooming_Dr.  
Zip/Postal Code:    32323

Also,  I need for the program to pause for each screen before proceeding to
the next one.  I tried using:
   IF EOLN(A) THEN
but it didn't work.

Can someone help?  Any suggestions?  Thank you very much...



Wed, 18 Jun 1902 08:00:00 GMT  
 Hooked on Text files...

Quote:
> Hello!  I need help with reading text files using Turbo Pascal 6.00
> Here's the text file that I'm reading from:
> (test.txt)

> Joe_Buster 1 433-2334 32_Blooming_Dr. 32323
> Gingo_Hill 2 322-2332 12_Dingo_Ln. 21223

> Here's what I got so far:
> var  A : text;
>      B : char;
> BEGIN
>    ASSIGN(A,'test.txt'); RESET(A);
>    WHILE NOT EOF(A) DO
>         BEGIN
>             read(A,B);

                     ^ you're reading character-by-character? Why?
Quote:
>             write(B);
>             IF B = ' ' THEN writeln;
>         END;
>    readln;

     ^^^^^^ This won't work, since you'vbe already reached end-of-file.

Quote:
> END.

> The output of my program would be:

> Joe_Buster
> 1
> 433-2334
> 32_Blooming_Dr.
> 32323
> Gingo_Hill
> 2
> 322-2332
> 12_Dingo_Ln.
> 21223

> Here's my problem:
> I need to figure out a way to store each character except for the space as
> a string.  I tried using the CONCAT command, but it didn't work.  I got the
> part where it writes each section (Name, Number, Phone Number, etc..) on
> different lines.  I can do this easily if the each data was on a separate
> line in the text file,  but  I don't want to do that.

> Here's the output I'm trying to get:

> Name :  Joe_Buster
> Number:  1
> Phone Number:  433-2334
> Home address:  32_Blooming_Dr.
> Zip/Postal Code:    32323

> Also,  I need for the program to pause for each screen before proceeding to
> the next one.  I tried using:
>    IF EOLN(A) THEN
> but it didn't work.

> Can someone help?  Any suggestions?  Thank you very much...

   Since you're using TP/BP, you might as well use strings (the full
text data record) and parse tokens from the whole record. I would do it
this way:

var S : string;
    I : integer;
...
  while not EOF (A) do
    begin
      readln (A,S);         { read entire text record }
      I := Pos(' ',S);         { find space in string }
      writeln (Copy(S,1,I);  { write 1st token (name) }
      Delete (S,1,I);                  { delete token }
      etc.                       { for rest of fields }
    end;
  Close (A)                        { close input file }



Wed, 18 Jun 1902 08:00:00 GMT  
 Hooked on Text files...

Quote:

>Hello!  I need help with reading text files using Turbo Pascal 6.00
>Here's the text file that I'm reading from:
>(test.txt)
>Joe_Buster 1 433-2334 32_Blooming_Dr. 32323
>Gingo_Hill 2 322-2332 12_Dingo_Ln. 21223

wanted output:

Quote:
>Name :  Joe_Buster
>Number:  1
>Phone Number:  433-2334
>Home address:  32_Blooming_Dr.  
>Zip/Postal Code:    32323
>Can someone help?  Any suggestions?  Thank you very much...


This "fully blown" program was written before I saw Mike Copeland's  
response to your query and is what he suggests with a few more bells
and whistles. You could still use your underline approach and use a
space as a delimiter if you wish. My preference is to use spaces
instead of underlines and to use a non-space delimiter.

Store the data with Writeln(file, data) and read out each line of text
with Readln(file, data) where "data" is a 255 Char String (or
shorter). After reading the line from disk, parse the line into
sub-strings using the chosen delimiter and put these sub-strings in an
array. You can now display the combination of the sub-strings'
identity with the sub-string since both of these entities are array
elements.

The same prompt strings are used when entering the data as identifiers
when displaying. The number count is entered by the computer. When
"field" data is not applicable, just press enter to skip to the next
field. During display, the prompts that were skipped will not be
shown.

The text file lines for your two examples would look like

Joe Buster\1\433-2334\\\32 Blooming Dr.\32323\\\
Gingo Hill\2\322-2332\\\12 Dingo Ln.\21223\\\

when the following program is used.

Regards,
Clif
-------------------------

Program TextData;

  Methods are shown to enter and read data from a textfile in which
  one line is used as a "data bank". Each "field" of the line of text
  uses a delimiter (d = '\') to separate the "field" fragments. This
  allows the fragments to contain spaces. Although the line number
  is stored within the line (as requested), updating with the DOS
  editor (which will handle 255 char strings) would be easier if the
  line count were left to the program. }

Uses CRT;

CONST
FileName = 'Test.Txt';
MaxPrompt = 9;
Prompt: Array [1..MaxPrompt] of String[20] =
        ('Name:               ',
         'Number:             ',
         'Phones:             ',
         'FAX:                ',
         'email:              ',
         'Address:            ',
         'City, St/Prov, Zip: ',
         'Country:            ',
         'Notes:              ');

d{limiter} = '\' ;

TYPE
s59 = String[59];  (* for a total of 79 chars on screen line *)
multi = Array[1..MaxPrompt] of s59;

VAR
f:Text;
Ch:Char;

Procedure Parse(ss:String; VAR fdata:multi);
(* Last char of ss is a delimiter *)
VAR i, p0, p1:Byte;
Begin
     i := 0;    p0 := 1;
     p1 := Pos(d, ss);   (* position of 1st delimiter *)
     While p1 > 0 Do
     Begin
          Delete(ss, p1, 1);    (* erase 1st remaining delimiter *)
          Inc(i);
          (* for blank fields, fdata[i] := '' *)
          fdata[i] := Copy(ss, p0, p1 - p0);
          p0 := p1;  (* p0 is now at first char of next field *)
          p1 := Pos(d, ss);  (* next delimiter, or 0 if finished *)
     End;
End;

(********************* READING DATA ********************)

Procedure ReadFile;
VAR  s:String;
     FileData:multi;
     n, Indx:Byte;
     K:Char;
Begin
     Assign(f, FileName);
     {$I-}     (* removes IO error abort *)
     Reset(f);
     {$I+}
     If IOResult <> 0 then
     Begin
          Writeln;
          Write(Filename, ' does not exist. Press <Enter>');
          Readln;
          Exit; (* procedure *)
     End;

     K := 'X';    (* arbitrary *)
     ClrScr;
     While (not eof(f)) and (K <> 'Q') do
     Begin
          Readln(f, s);
          Parse(s, FileData);
          For n := 1 to MaxPrompt Do If FIleData[n] <> '' Then
                    Writeln(Prompt[n], FileData[n]);
          Writeln;
          WriteLn('Any key for more. Q to Quit: ');
          K := UpCase(ReadKey);
          Writeln;
     End;
     Close(f);
End;

(*********************** ENTERING DATA *******************)

Procedure MakeFile;
Label Finis;
VAR  dStr:String;
     Entry:s59;
     m:Byte;
     Ct:Word;
     Ans:Char;
Begin
     ClrScr;
     Ct := 0;
     Assign(f, FileName);
     {$I-}
     Reset(f);
     {$I+}
     If IOResult = 0 then  (* count number of text lines *)
     Begin
          While not eof(f) do
          Begin
               Readln(f);
               Inc(Ct);
          End;
          Append(f);
     End
     Else Rewrite(f);

Repeat
     Repeat
          Writeln;
          Writeln('Enter Q (or q) at any time to return to menu.');
          Writeln;
          dStr := '';
          For m := 1 to MaxPrompt Do
          Begin
              Write(Prompt[m]);
              If m <> 2 then Readln(Entry)
              Else   (* insert line number *)
              Begin
                   Inc(Ct);
                   STR(Ct, Entry);
                   Writeln(Entry);
              End;
              If (Entry = 'Q') OR (Entry = 'q') then Goto Finis;
              dStr := dStr + Entry + d ;  (* appends delimiter *)
          End;
          Writeln;
          Writeln('If correct, press <Enter>, other to redo or
quit.');
          Ans := ReadKey;
     Until Ans = Chr(13);

     Writeln(f, dStr);
Finis:
Until (Entry = 'Q') OR (Entry = 'q');
      Close(f)
End;

(******************** MAIN PROGRAM *********************)

BEGIN                      
  Repeat
    Repeat
        ClrScr;

        Writeln('1.  Read Text File');
        Writeln('2.  Make/Extend Text File');
        Writeln('Q.  Quit');
        Writeln; Write('   >>>> Press 1, 2 or Q: ');
        Ch := UpCase(ReadKey);
        If NOT (Ch in ['1', '2', 'Q']) then Write(Chr(7));
    Until Ch in ['1', '2', 'Q'];

    If Ch = '1' then ReadFile;
    If Ch = '2' then MakeFile;
  Until Ch = 'Q';
END.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Unix Text File ---> DOS Text File

2. Converting Non-Text File to Text File

3. .MAP file format and overlay debugger hook

4. ADDING TEXT TO A TEXT FILE

5. Search for text string in a text file

6. Search for text string in a text file

7. Text File Read / Dbase file Write

8. Converting binary (text file) to compress binary file

9. Converting ASCII Text file to compress binary file format

10. File lister/editor for huge text files

11. Text file to Type File!

12. Assess text file info using ttAscii and Schema file

 

 
Powered by phpBB® Forum Software