Board index » delphi » Lil help with this program im making

Lil help with this program im making

Alright, basically im trying to get this program to exchange characters
with numbers. As i found out, the array that i used wont let me go over
251, so i had to comprimise by doing loops and stuff as you will see below.
Basically what it should do is read each character, search for certain
ones, then replace them with a new character (ie: a becomes 4). I tried
running it just to print to the screen and not a file, and that works fine.
Yet, when it tries to write to a file, i get errors saying the file isnt
open.
Basically since im adding onto the file i need to use rewrite, then append
all the other times, yet im still getting errors.
Blah, i will stop talking, you guys will figure out what i mean when you
look over the code, sorry for the sloppy programming.

-- cut here --

program search;
 uses crt;

  type
    characters = array [1..200] of char;

  var
    name,letters : string;
    filez : text;
    let :char;
    charac : characters;
    a,x,anint,anint2,i, count: integer;

begin

  write('Please enter the file name:');
   readln(name);

    Assign(filez,name);
     reset(filez);

  count := 0;
  while not EOF(filez) do
    begin
      read(filez, let);
       count := count + 1;
    end;
    write(count);

   a := count div (200);
    if count mod (200) <> 0 then
      a := a + 1;
        reset(filez);
        anint := 1;
        anint2:= 200;
    for x := 1 to a do
      begin
        for i := 1 to 200 do
          begin
           read(filez, charac[i]);
           case charac[i] of
            'i' : charac[i] := '1';
            'o' : charac[i] := '0';
            'e' : charac[i] := '3';
            'a' : charac[i] := '4';
          end;
        write(charac[i]);
      end;
    readln;

    if x = 1 then
      rewrite(filez)
       else
        append (filez);
    for i := anint to anint2 do
      begin
       write(filez, charac[i]);
       writeln;
      end;
       anint:= anint + anint2;
       anint2 := anint + 200;
            end;
  close(filez);
  writeln(count, ' characters found.');
  repeat until keypressed;
end.

-- end cut ---

notice how the "write(charac[i]);" will display all the stuff on the
screen. everything below the "if x = 1 then" statement is basically for
writing to the file. This is where im having some trouble, it wont write
more then 200 characters to the file, even though i have attempted ways
around it.
Any suggestions would be greatly appreciated.
- Josh

 

Re:Lil help with this program im making


hi josh,

"Josh Gilmour" <d...@sinfo.net.nospam> schrieb im Newsbeitrag
news:Xns902BC20A083F8dewzsinfonet@24.128.8.70...

Quote
> Alright, basically im trying to get this program to exchange
characters
> with numbers. As i found out, the array that i used wont let me go
over
> 251, so i had to comprimise by doing loops and stuff as you will see
below.
> Basically what it should do is read each character, search for certain
> ones, then replace them with a new character (ie: a becomes 4). I
tried
> running it just to print to the screen and not a file, and that works
fine.
> Yet, when it tries to write to a file, i get errors saying the file
isnt
> open.
> Basically since im adding onto the file i need to use rewrite, then
append
> all the other times, yet im still getting errors.
> Blah, i will stop talking, you guys will figure out what i mean when
you
> look over the code, sorry for the sloppy programming.

[...]

your code really will have minimum performance.

you can't work on textfiles the way your're trying - there is no random
access on textfiles.

try to use blockread and blockwrite.

if you must use read/write on textfiles you'll have to work with 2
files.
read one and write to the other. then kill the file you read from and
rename the new one.

here might be another problem if you try to handle with binary files.
there might be ^Z (#26) in the file that will signal an EOF before the
whole file is examined.

try this for random access - untestet !!!

program ex;

var
  f: file;
  w,i :word;
  fn :String;
  cod: array[#0..#255 ] of char;
  buf: array[0..4095] of char;

begin
  writeln('Transforming files');
  write('filename ? ');
  readln(fn);
  assign(f,fn);
  {$i-}
  reset(f,1); { open file for random access}
  if ioresult<>0 then halt; {error occured}
  for i:=0 to 255 do cod[chr(i)]:=chr(i);
  cod['i']:='1';
  cod['o']:='0';
  cod['e']:='3';
  cod['a']:='4';
  repeat
     blockread(f,buf,sizeof(buf),w);
     if w>0 then
     begin
       seek(f,filepos(f)-w); { go back }
       for i:=0 to w-1 do buf[i]:=cod[buf[i]]; { exchange }
       blockwrite(f,buf,w,i);
       if i<>w then w:=0 {error}
     end
  until w<>sizeof(buf);
  if filepos(f)=filesize(f) then
    writeln('Done')
  else
   writeln('Error');
  close(f);
  write('Enter.');
  readln
end.

hth
jochen

Re:Lil help with this program im making


Quote
Josh Gilmour wrote:

> Alright, basically im trying to get this program to exchange characters
> with numbers. As i found out, the array that i used wont let me go over
> 251, so i had to comprimise by doing loops and stuff as you will see below.
> Basically what it should do is read each character, search for certain
> ones, then replace them with a new character (ie: a becomes 4). I tried
> running it just to print to the screen and not a file, and that works fine.
> Yet, when it tries to write to a file, i get errors saying the file isnt
> open.
> Basically since im adding onto the file i need to use rewrite, then append
> all the other times, yet im still getting errors.
> Blah, i will stop talking, you guys will figure out what i mean when you
> look over the code, sorry for the sloppy programming.

> -- cut here --

> program search;
>  uses crt;

>   type
>     characters = array [1..200] of char;

>   var
>     name,letters : string;
>     filez : text;
>     let :char;
>     charac : characters;
>     a,x,anint,anint2,i, count: integer;

> begin

>   write('Please enter the file name:');
>    readln(name);

>     Assign(filez,name);
>      reset(filez);

>   count := 0;
>   while not EOF(filez) do
>     begin
>       read(filez, let);
>        count := count + 1;
>     end;
>     write(count);

>    a := count div (200);
>     if count mod (200) <> 0 then
>       a := a + 1;
>         reset(filez);
>         anint := 1;
>         anint2:= 200;
>     for x := 1 to a do
>       begin
>         for i := 1 to 200 do
>           begin
>            read(filez, charac[i]);
>            case charac[i] of
>             'i' : charac[i] := '1';
>             'o' : charac[i] := '0';
>             'e' : charac[i] := '3';
>             'a' : charac[i] := '4';
>           end;
>         write(charac[i]);
>       end;
>     readln;

>     if x = 1 then
>       rewrite(filez)
>        else
>         append (filez);
>     for i := anint to anint2 do
>       begin
>        write(filez, charac[i]);
>        writeln;
>       end;
>        anint:= anint + anint2;
>        anint2 := anint + 200;
>             end;
>   close(filez);
>   writeln(count, ' characters found.');
>   repeat until keypressed;
> end.

> -- end cut ---

> notice how the "write(charac[i]);" will display all the stuff on the
> screen. everything below the "if x = 1 then" statement is basically for
> writing to the file. This is where im having some trouble, it wont write
> more then 200 characters to the file, even though i have attempted ways
> around it.
> Any suggestions would be greatly appreciated.
> - Josh

After a rewrite a file is created or if it existed it is eliminated and
set to empty. So when x = 1 the file is created anew and has 200
characters written to it, then when x=2 this program trys to read
another 200 characters but it is at the end of the newly created file!

It the output file is made different from the input file then something
like the following might solve some of the problems with this program.

if x = 1
then begin
     assign (fOut,'ANAME');
     rewrite (fOut);
     end
else begin
     {do nothing, append is no longer needed}
     end;
Don't forget to change write(filez, charac[i]) to write(fOut, charac[i])
and of course close (fOut) at the end.

Regards Hanford

Other Threads