Link to home
Start Free TrialLog in
Avatar of jrmn
jrmn

asked on

Read/Write TXT file

Hi Experts,

Given a TXT file (NAMES.TXT) containing the following lines separated by comma:

NAMES.TXT
--------------

Jay, 31, 01/04/73,M
Joe,20,05/10/04,M
Mary,4,03/01/00,F
Jane,10,10/10/94,F
Clark,2,09/20/02,M
Rose,5,08/15/99,F

Note: All these entries(records) are terminated by enter (courage return)

Is there a way to read the contains of this file, add a couple of words and save it to another TXT file (BIO.TXT)?
The BIO.TXT will contain lines like these:


BIO.TXT
------------

Hi I'm Jay. I was born on 01/04/73. I'm 31 years old Male.
Hi I'm Joe. I was born on 05/10/84. I'm 20 years old Male.
Hi I'm Mary. I was born on 03/01/00. I'm 4 years old Female.
Hi I'm Jane. I was born on 10/10/94. I'm 10 years old Female.
Hi I'm Clark. I was born on 09/20/02. I'm 2 years old Male.
Hi I'm Rose. I was born on 08/15/99. I'm 5 years old Female.

Note: Gender if M = Male ; F = Female

Can this be done?

I'm using delphi 6

Thanks,
jrmn

ASKER CERTIFIED SOLUTION
Avatar of geobul
geobul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mugahied
Mugahied

firstly u must declare 4 const

name:'Hi I'm ';
born:'. I was born on ';
me:'. I'm ';
years:' years old ';

and u need 2 textfile
var
file1,file2:textfile;
to read from file u must have an string variable declard
data:string;
and also u must have an integer variable to control the length of line
x:integer;

also u need name1,born1,years1,sex:string;

now we can start--->

assignfile(file1,'{Fullpath+filename(NAMES.TXT)}');
{$-I}
reset(file1);
{$+I}
if IOResult=0 then
begin
   assignfile(file2,'{Fullpath+filename(BIO.TXT)}');
   rewrite(file2);
   while not EOF(file1)do
   begin
      x:=1;
      name1:='';
      born1:='';
      years1:='';
      sex:='';
      readln(file1,data);
      while (data[x]<>',') do
      begin
         name1:=name1+data[x];
         inc(x);
      end
      x:=x+2;
      while (data[x]<>',') do
      begin
         years1:=years1+data[x];
         inc(x);
      end
      x:=x+2;
      while (data[x]<>',') do
      begin
         born1:=born1+data[x];
         inc(x);
      end
      inc(x);
      if data[x]='M' then
        Sex:='Male.'
      else
        Sex:='Female.';
      data:=name+name1+born+born1+me+years1+years+sex;
      writeln(file2,data);  
   end
   closefile(file1);
   closefile(file2);
end else
   ShowMessage('The File Named NAMES.TXT was not Found');

Mugahied, your code has one serious problem: if there is one wrong line (with less than three commas) you're entering in neverending loop or an exception will be raised accessing data[x] where x > length(data).
SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oops, birthdate and age are in other order

replace this (which was also wrong (copy/paste error))
      OutList.Add(Format(BioText,[WorkList[0],WorkList[3],WorkList[3],WorkList[3]]);
 into
      OutList.Add(Format(BioText,[WorkList[0],WorkList[2],WorkList[1],WorkList[3]]);
 
meikl ;-)