Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Programming in Pascal

Posted on 2003-02-25
15
Medium Priority
?
335 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:thehaze
15 Comments
 
LVL 101

Expert Comment

by:mlmcc
ID: 8018269
You can't read from the file and write to at the same time.

mlmcc
0
 
LVL 7

Expert Comment

by:billious
ID: 8018336
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
 
LVL 15

Expert Comment

by:VGR
ID: 8019128
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:thehaze
ID: 8020086
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
 
LVL 15

Expert Comment

by:VGR
ID: 8020539
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
 
LVL 15

Expert Comment

by:VGR
ID: 8020545
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
 
LVL 6

Accepted Solution

by:
My name is Mud earned 400 total points
ID: 8023445
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
 
LVL 6

Expert Comment

by:My name is Mud
ID: 8023458
If you want explanation on this kind of voodoo just say so...
0
 
LVL 15

Expert Comment

by:VGR
ID: 8023565
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
 
LVL 6

Expert Comment

by:My name is Mud
ID: 8023596
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
 
LVL 15

Expert Comment

by:VGR
ID: 8023922
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
 
LVL 15

Expert Comment

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

period.
0
 
LVL 6

Expert Comment

by:My name is Mud
ID: 8024306
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
 
LVL 15

Expert Comment

by:VGR
ID: 8024665
not at all, I even usually give all the question's points and merit to anybody who complains in such a pityful way ;-)
0
 
LVL 6

Expert Comment

by:My name is Mud
ID: 8024721
yes sr...
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article explains the process to deploy a Self-Service password reset portal I developed a few years ago. Hopefully, it will prove useful to someone.  Any comments, bug reports etc. are welcome...
How do you create a user-centered user experience on your website? And what are some things you should consider in the process?
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question