Written some cool source code? Upload it to Programmer's Heaven.

Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 3714
Number of posts: 12954

This Forum Only
Post New Thread

Report
Read from a file. Posted by Nenad4o on 21 Nov 2009 at 9:26 AM
Hello. I have a problem with reading from a file. The filename contains numbers and pascal doesn't allow it. It says that the file is not found. If I rename it to only letters, it works. How can I fix this? (e.x. filename: 070321650.txt)


Report
Re: Read from a file. Posted by Atex on 21 Nov 2009 at 3:45 PM
: Hello. I have a problem with reading from a file. The filename
: contains numbers and pascal doesn't allow it. It says that the file
: is not found. If I rename it to only letters, it works. How can I
: fix this? (e.x. filename: 070321650.txt)


What compiler and OS are you using ? TP7 supports all sorts of filenames but cannot open long names by default. For example 070321650.txt probably has a short dos name of 070321~1.txt, trying that will most likely fix your problem. You could also try the following unit to have long filename support:
{ Compiler BP/TP7 }

UNIT W95;

INTERFACE

{$A+,D-,G+,L-,O-,X+}

type
   tSearchRec = record
      Attr: longint;
      Creation: comp;
      LastAccess: comp;
      LastModification: comp;
      HighFileSize: longint;
      LowFileSize: longint;
      Reserved: comp;
      Name: array[0..259] of char;
      ShortName: array[0..13] of char;
      Handle: word;
   end;

const
   faReadOnly      =  $01;
   faHidden        =  $02;
   faSysFile       =  $04;
   faVolumeID      =  $08;
   faDirectory     =  $10;
   faArchive       =  $20;
   faAnyFile       =  $3F;

   FUNCTION Win95Loaded: boolean;
   FUNCTION GetLongName(FName: string): string;
   FUNCTION FindFirst(FileSpec: string; Attr: word; var S: tSearchRec): integer;
   FUNCTION FindNext(var S: tSearchRec): integer;
   FUNCTION FindClose(var S: tSearchRec): integer;


var WIN95SYSTEM: boolean;

IMPLEMENTATION

uses dos,strings;


{==========================================================================}
 FUNCTION Win95Loaded: boolean;
{--------------------------------------------------------------------------}
var
   Ver: word;
   P: pChar;

   {-------------------------------------------------------------}
   function GetAnyCaseEnv(VarName: pChar): pChar;
   var
      L: word;
      P: pChar;

   begin
      L := StrLen(VarName);
      P := Ptr(Word(Ptr(PrefixSeg,$2C)^),0);
      while P^ <> #0 do begin
         if (StrLIComp(P,VarName,L) = 0) and (P[L] = '=') then begin
            GetAnyCaseEnv := P + L + 1;
            Exit;
         end;
         Inc(P,StrLen(P) + 1);
      end;
      GetAnyCaseEnv := Nil;
   end;
   {-------------------------------------------------------------}
   function GetCurLongDir(Dir: pChar; Drive: byte): pChar; assembler;
   asm
      mov al,Drive
      or al,al
      jne @@1
      mov ah,19h
      int 21H
      inc ax
    @@1: mov dl,al
      push ds
      lds si,Dir
      push ds
      push si
      mov byte ptr es:[si],0
      mov ax,7147h   { note AH=$71, AL=$47, not AH=$47 as in standard DOS }
      int 21h
      pop ax
      pop dx
      pop ds
   end;
   {-------------------------------------------------------------}

begin
   Win95Loaded := False;
   Ver := DosVersion;
   if (Lo(Ver) = 7) and (Hi(Ver) = 0) then begin
      if GetAnyCaseEnv('winbootdir') <> Nil then begin
         GetMem(P,256);
         if StrLen(GetCurLongDir(P,0)) > 0 then Win95Loaded := True;
         FreeMem(P,256);
      end;
   end;
end; { Win95Loaded }

{==========================================================================}
 FUNCTION FindFirst(FileSpec: string; Attr: word; var S: tSearchRec): integer;
{--------------------------------------------------------------------------}
begin
   FileSpec := FileSpec + #0;
   S.Attr := Attr;
   asm
      push ds
      push ss
      pop  ds
      lea  dx,filespec+1
      les  di,S
      mov  ax,$714e
      mov  cx,attr
      mov  si,0
      int  $21
      les  di,S
      mov  word ptr es:[di+TSearchRec.handle], ax
      jc  @1
      xor  ax,ax
     @1:
      mov @result,ax
      pop  ds
   end;
end;

{==========================================================================}
 FUNCTION FindNext(var S: tSearchRec): integer;
{--------------------------------------------------------------------------}
begin
   asm
      mov  ax,$714f
      mov  si,0
      les  di,S
      mov  bx,word ptr es:[di+tSearchRec.Handle]
      int  $21
      jc  @1
      xor  ax,ax
     @1:
      mov @result,ax
   end;
end;

{==========================================================================}
 FUNCTION FindClose(var S: tSearchRec): integer;
{--------------------------------------------------------------------------}
begin
   asm
      mov  ax,$71a1
      les  di,S
      mov  bx,word ptr es:[di+tSearchRec.Handle]
      int  $21
      jc  @1
      xor  ax,ax
     @1:
      mov @result,ax
   end;
end;

{==========================================================================}
 FUNCTION GetLongName(FName: string): string;
{--------------------------------------------------------------------------}
var
   SR: tSearchRec;
   Res: Integer;

begin
   GetLongName := '';
   Res := FindFirst(FName,faAnyFile-faVolumeID,SR);
   if (Res = 0) and (SR.ShortName[0] <> #0) then begin
      if (SR.Name[0] <> #0) then GetLongName := StrPas(SR.Name);
   end;
   FindClose(SR);
end;


BEGIN
   if Win95Loaded then WIN95SYSTEM := True else WIN95SYSTEM := False;
END.


Report
Re: Read from a file. Posted by Nenad4o on 22 Nov 2009 at 5:37 AM
I'm using WinXP and TP7. About the unit: How should i rename the extension? TPU? And should i change something in it so it works? Or just add it to the program like "uses longname;"?
Report
Re: Read from a file. Posted by Atex on 22 Nov 2009 at 9:29 PM
: I'm using WinXP and TP7. About the unit: How should i rename the
: extension? TPU? And should i change something in it so it works? Or
: just add it to the program like "uses longname;"?
:

Compile the unit first, set destination: disk, it should generate a TPU file, then add it to the program: "uses w95;" First scan for text files to get the short names, use the getlongname function to find out the long name, see if it matches, then open it the usual way using the short name.



 
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A BootstrapLabs project.