Programming in Pascal

I have the following program written in Pascal that:

1. Reads an ASCHI input file.
2. Searchs for " and replace with blanks.
3. Searchs for "," and replace with ";"
4. Creates new ASCHI file called FileClean.asc

How do I edit the following code, so that the output file automatically takes the name of the input file + extension 'asc'.

For example. Inputfile is called abc.asc -> output file abc.asc
             Inputfile is called def.asc -> output file def.asc
             etc..

The present code looks like this:

Program FileCln;
var
 F1,F2 : File of char;
 c1    : Char;

begin
IF Paramcount<1 Then
begin
  WriteLN('Syntax : FileCln <InputFile>');
  Halt(255);
end;
Assign(F1, Paramstr(1));
Reset(F1);
Assign(F2, 'FileCln.asc');
Rewrite(F2);
While Not EOF(F1) Do
begin
  Read(F1,C1);
  Case C1 OF
    '"' : C1:=#32;
    ',' : C1:=';';
  end;
  Write(F2,C1);
end;
end.
thehazeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mlmccCommented:
You can't read from the file and write to at the same time.

mlmcc
0
billiousCommented:
if pos('.',paramstr(1)) > 0 then
  filename2 := copy(paramstr(1),1,pred(pos('.',paramstr(1))))
  filename2 := paramstr(1);

Assign(F2, filename2 + '.asc');

or, of course, you could add the '.asc' to filename2 (which should be declared as a string) rather than adding it in the assign statement.

You may also want to check that paramstr(1) DOESN'T end '.asc' - otherwise you'll try to open the same physical file twice :)
[try copy(paramstr(1),pos...,255) = 'asc')]

next step - take care of case (upcase function may be a start...)

...Bill
0
VGRCommented:
pred(pos()) arf arf arf good ol' strange way to write pos()-1 ;-)

missing "else" above

other than this, it should work.

for mlmcc, you misread
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

thehazeAuthor Commented:
How would the finished modified code look.? Ideally it was my intention to use this program to process a batch of ascii files. Something like filecln.exe *.*, which would then result in a list of formatted ascii files all ending in filename.asc
0
VGRCommented:
well, it should be something like this :

Program FileCln;
var
F1,F2 : File of char;
c1    : Char;
filename2 : String;
z : Integer;

begin
// refuse if no parameter
IF Paramcount<1 Then
begin
 WriteLN('Syntax : FileCln <InputFile>');
 Halt(255);
end;
// assign
z:=pos('.',paramstr(1));
if z > 0 then filename2 := copy(paramstr(1),1,z-1)
 else filename2 := paramstr(1);
Assign(F2, filename2 + '.asc');
Assign(F1, Paramstr(1));
// read, parse, write
Reset(F1);
Rewrite(F2);
While Not EOF(F1) Do
begin
 Read(F1,C1);
 Case C1 OF
   '"' : C1:=#32;
   ',' : C1:=';';
 end;
 Write(F2,C1);
end;
end.

This processes a single file.
To process a bunch of ASCII files, you'll have to use FindFirst, FindNext, a TSearchRecord and FindClose (see documentation on the FindFirst function)
0
VGRCommented:
BTW, "you" forgot to close the files ; it's important for the file being written to 8-)
Add this just before the final "end." :

Close(F1);
Close(F2);
0
My name is MudSystems EngineerCommented:
This Proggy Reads all the *.ASC files in the actual directory and replace the chars ',' by ';' and '"' by '<space>' opening the single file... which Can't be done in Pascal... if you don't use voodoo stuffs...


Program RewriteASCFiles;

Uses
  DOS,CRT;

Const
  Files = '*.ASC';
  BufferSize = 1024;

Var
  TheFile: File;
  TheBuffer: Array[0..1023] Of char;
  IOError: Integer;
  AddressFile: LongInt;
  FileList: SearchRec;
  SizeRead: Word;

Procedure ReplaceTheChars(Var Buf; Size: Integer);
Var
  Count: Integer;
Begin
  For Count:=0 To Size-1 Do
    Begin
      If Char(Ptr(Seg(Buf),Ofs(Buf)+Count)^) = '"' Then
        Char(Ptr(Seg(Buf),Ofs(Buf)+Count)^):=' '
      Else If Char(Ptr(Seg(Buf),Ofs(Buf)+Count)^) = ',' Then
        Char(Ptr(Seg(Buf),Ofs(Buf)+Count)^):=';'
    End
End;

Begin
  FindFirst(Files,ARCHIVE,FileList);
  While DOSERROR = 0 Do
    Begin
      Assign(TheFile,FileList.Name);
      {$I-} Reset(TheFile,1); {$I+}
      IOError:=IOResult;
      If IOError <> 0 Then
        Begin
          Halt
        End;
      AddressFile:=0;
      Repeat
        Seek(TheFile,AddressFile);
        BlockRead(TheFile,Ptr(Seg(TheBuffer),Ofs(TheBuffer))^,BufferSize,SizeRead);
        ReplaceTheChars(TheBuffer, SizeRead);
        Seek(TheFile,AddressFile);
        BlockWrite(TheFile,TheBuffer,SizeRead);
        AddressFile:=AddressFile+SizeRead
      Until SizeRead = 0;
      Close(TheFile);
      FindNext(FileList);
    End;
End.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
My name is MudSystems EngineerCommented:
If you want explanation on this kind of voodoo just say so...
0
VGRCommented:
no Voodoo here, just an untyped file opened in ReadWrite mode thanks to Block operations (coming from UCSD Pascal, by the way) ;-))

Not bad, but a bit overkill given that the code I've modified for him is also fgunctional and more user-friendly ;-)

Don't you forget the FindClose() at the end ? ;-)
and this :
If IOError <> 0 Then
       Begin
         Halt
       End;

is the begin..end block useful ?
0
My name is MudSystems EngineerCommented:
What did I ever Do to you??? oh well...


>>...coming from UCSD Pascal...

Never hear of it... but I would say it comes from Borland... since Pascal is almost as made by Borland...



>>more user-friendly ;-)

??? WTF??? are you in drugs??? a single outputed line is more user-friendly???




>>Don't you forget the FindClose() at the end ? ;-)

You're not on the Delphi forum here... this is PASCAL... and in pascal there's no FindClose... period... make that three periods...



>>is the begin..end block useful ?

It is as useful as the 255 exit code in your code...

>>Halt(255);



There so don't give me no Fsking crap here... and besides you never answer my question on this thread... http://www.experts-exchange.com/Programming/Programming_Languages/Pascal/Q_20465314.html

Why Cheat???
0
VGRCommented:
cool down, there is nothing personal.
The halt(255) comes not from me, but anyway I think TP sends out the errorlevel corresponding to the x in Halt(x) so yes, it's useful to check the actuel reason for termination, once outside of the DOS executable (in a batch, for instance)

I didn't criticize your code, I just said that assign/reset/rewrite/close is more user-friendly than blockread/buffers/FSeek, and I hope you'll agree with me 8-)

Oh, and I had a look in the referenced other message. I maintain 100% what I wrote. BlockRead/Write "is NOT for standard [text] files" and people (let's say : beginners) can't do writeln/readln they were accustomed to with an untyped file. That's all and it's reasonable.

Again, nothing personal. I find you a bit paranoid :D

0
VGRCommented:
and for "cheat" explained : the asker was expecting a solution based on standard TextFile I/O, not on untyped file Block IO

period.
0
My name is MudSystems EngineerCommented:
I state what I said... Useless...

user-friendly... User... the one who uses the terminated program, not the programmer...

There's no Standard procedures to do a job... you just do it...

>>the asker was expecting a solution based on standard TextFile I/O

Would you point me to where he state that???



Even thought you are wrong, you have to be right...

I find you quite Point-horny on the way you try to discredit anyones answers...
0
VGRCommented:
not at all, I even usually give all the question's points and merit to anybody who complains in such a pityful way ;-)
0
My name is MudSystems EngineerCommented:
yes sr...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Pascal

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.