• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 141
  • Last Modified:

File read and replace line blank strings !

I need someone to write me the folowing:

I have a txt file in which i have for example:
word1 word44 wrod66
word31 word4334
word21 word444 wrod656 wrod366
word1
word44 wrod66

I need a code which will:
- read that file
- replace " " with "." (so i need to get word1.word44.wrod66)
- save line with raplaced spaces to a variable.
0
65zgtre45rr
Asked:
65zgtre45rr
  • 5
  • 3
  • 2
  • +1
3 Solutions
 
2266180Commented:
well.. something like this should do it:

procedure TForm1.FormCreate(Sender: TObject);
var s,t:TStringList; i:integer; r:string;
begin
  s:=TStringList.create;
  t:=TStringList.create;
//  s.loadfromfile('the text file with those words');// uncomment this and delete the below lines
s.Text:=
'word1 word44 wrod66'#13#10+
'word31 word4334'#13#10+
'word21 word444 wrod656 wrod366'#13#10+
'word1'#13#10+
'word44 wrod66';
  for i:=1 to s.count do
  begin
    r:=s[i-1];
    r:=StringReplace(r,' ','.',[rfReplaceAll]);
    if r<>s[i-1] then// did we do at least one replace?
      t.add(r);
  end;
  s.free;
  // do whatever with the lins saved
  t.free;
end;

since tere will be more lines replaced, aI put them all in a string list. if you want just a string variable then replace the addition in the string list with a concatenation
0
 
Scay7Commented:
Heres another way :P
place 1 memo, 1 button and 1 opendialog on form...

procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.Clear;
opendialog1.Execute;
memo1.Lines.LoadFromFile(opendialog1.FileName)
end;

procedure TForm1.Button1Click(Sender: TObject);
var
done, lines : string;
f : textfile;
begin
memo1.Clear;
assignfile(f,opendialog1.FileName);
reset(f);
  while not eof(f) do
  begin
  readln(f,lines);
  done := StringReplace(lines,' ','.',[rfReplaceAll]);
  memo1.Lines.Add(done)
  end;
closefile(f);
end;


Peace Scay7
0
 
Scay7Commented:
Example of output :

f q324 t q34 t 34 6t 341q 6 1324 6 13246 13 426
21 45 123 45 13 4 513 45 34 5 1 451 345 134 5
 3245 32145` 2 56`2 65 `2 46 `246 32 46 334 53
3  34532 45  3  3 3       2134 21    23 4 2 6fd sg d
 2452 146 4612 2146 6142 6 4264 fdg df dg dgg

f.q324.t.q34.t.34.6t.341q.6.1324.6.13246.13.426
21.45.123.45.13.4.513.45.34.5.1.451.345.134.5
.3245.32145`.2.56`2.65.`2.46.`246.32.46.334.53
3..34532.45..3..3.3.......2134.21....23.4.2.6fd.sg.d
.2452.146.4612.2146.6142.6.4264.fdg.df.dg.dgg

Peace Scay7
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
65zgtre45rrAuthor Commented:
This is ok, thank you. And could you just write me somthing to avoid program crash if user didn't selecet file in FileDialog.

Example:
if user select file in filedialog open file
else don't pen file
0
 
Scay7Commented:
he he just add this part....

memo1.Clear;
opendialog1.Execute;
memo1.Lines.LoadFromFile(opendialog1.FileName)
if not opendailog1.filename = '' then
begin
// put what ever here if user didnt select file...
end else
      begin
      //put what ever here if user did select a file...
      end;

Peace Scay7
0
 
65zgtre45rrAuthor Commented:
i have try this before and problem is that i'm getting error:

Operator not applicable to this operand type.

I'm using borlan delphi 2005
0
 
2266180Commented:
which line of code is that?
0
 
TheRealLokiSenior DeveloperCommented:
(just tweaking Scay7's code, his points)

procedure TForm1.Button1Click(Sender: TObject);
var
  sl: TStringList;
begin
  if opendialog1.Execute then // only do the following if the user selected a file
  try
    sl := TStringList.Create;
    sl.LoadFromFile(OpenDialog1.filename);
    memo1.Lines.Text := StringReplace(sl.Text,' ', '.', [rfReplaceAll]);
  finally
    sl.Free;
  end;
end;

0
 
65zgtre45rrAuthor Commented:
It works now.
0
 
TheRealLokiSenior DeveloperCommented:
should have been scay's points :-)
0
 
Scay7Commented:
(pirate with one eye) errrr me matie what thee steal me treasure :P

TheRealLoki ill post a message in HELP to re-assign points...

Peace Scay7
0
 
Scay7Commented:
Ah never mind, to lazy i got 900 so im cool...

Peace Scay7
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now