operation with files - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

operation with files

Mahiza B.Mahiza B. Posts: 2Member
Hi! how can i operate with files? i.e how i can copy files from other storages like cd,disk,etc to make new files in pascal?

Comments

  • zibadianzibadian Posts: 6,349Member
    : Hi! how can i operate with files? i.e how i can copy files from other storages like cd,disk,etc to make new files in pascal?
    :
    You can make new files by Assign()ing a filename to file/text variable, and then call Rewrite(). Example:
    [code]
    var
    f: file;
    begin
    Assign(f, 'somefile');
    Rewrite(f);
    Close(f);
    end;
    [/code]
    To copy a file to a normal writable media (ie disk/floppy/mem-stick), Open the source file for reading (Reset() instead of Rewrite()), and open/create the destination file for writing. Then read a certain amount of bytes from the source into a buffer and write that buffer to the destination. Here is a very simple and slow example:
    [code]
    var
    source, dest: file of byte;
    Buf: byte;
    begin
    Assign(source, 'sourcefile');
    Assign(dest, 'destfile');
    Reset(source);
    Rewrite(dest);
    while not eof(source) do
    begin
    read(source, Buf);
    write(dest, Buf);
    end;
    Close(source);
    Close(dest);
    end;
    [/code]
    You cannot use this method to write a file to CD, because the entire writing process is different. To write a file to CD, you'll probably need to import a COM object to handle to actual burning of the CD, and write the buffer to that object.
  • GaashiusGaashius Posts: 349Member
    : : Hi! how can i operate with files? i.e how i can copy files from other storages like cd,disk,etc to make new files in pascal?
    : :
    : You can make new files by Assign()ing a filename to file/text variable, and then call Rewrite(). Example:
    : [code]
    : var
    : f: file;
    : begin
    : Assign(f, 'somefile');
    : Rewrite(f);
    : Close(f);
    : end;
    : [/code]
    : To copy a file to a normal writable media (ie disk/floppy/mem-stick), Open the source file for reading (Reset() instead of Rewrite()), and open/create the destination file for writing. Then read a certain amount of bytes from the source into a buffer and write that buffer to the destination. Here is a very simple and slow example:
    : [code]
    : var
    : source, dest: file of byte;
    : Buf: byte;
    : begin
    : Assign(source, 'sourcefile');
    : Assign(dest, 'destfile');
    : Reset(source);
    : Rewrite(dest);
    : while not eof(source) do
    : begin
    : read(source, Buf);
    : write(dest, Buf);
    : end;
    : Close(source);
    : Close(dest);
    : end;
    : [/code]
    : You cannot use this method to write a file to CD, because the entire writing process is different. To write a file to CD, you'll probably need to import a COM object to handle to actual burning of the CD, and write the buffer to that object.
    :
    Zibadian maybe you have misread something. Our friend wants to write FROM Cd. Also I should note that this "pascal" maybe a Turbo or something similar, where COM objects do not exist.

    Here is a detection code, this would come in handy:
    [code]
    function iscdrom(drive:char):Boolean;
    var regs:registers;
    begin
    cdromfn:=false;
    if swap(dosversion)<$0200 then exit;
    drive:=upcase(drive);
    if(drive<'A') or (drive>'Z') then Exit;
    fillchar(regs,sizeof(regs),0);
    regs.cx:=ord(drive)-ord('A');
    regs.ax:=$150B;
    intr($2F,regs);
    iscdrom:=(regs.ax<>0) and (regs.bx=$ADAD);
    end;
    [/code]

    ****************
    Any questions? Just ask!

    :-) [b][blue]GAASHIUS[/blue][/b] :-)


  • zibadianzibadian Posts: 6,349Member
    : : : Hi! how can i operate with files? i.e how i can copy files from other storages like cd,disk,etc to make new files in pascal?
    : : :
    : : You can make new files by Assign()ing a filename to file/text variable, and then call Rewrite(). Example:
    : : [code]
    : : var
    : : f: file;
    : : begin
    : : Assign(f, 'somefile');
    : : Rewrite(f);
    : : Close(f);
    : : end;
    : : [/code]
    : : To copy a file to a normal writable media (ie disk/floppy/mem-stick), Open the source file for reading (Reset() instead of Rewrite()), and open/create the destination file for writing. Then read a certain amount of bytes from the source into a buffer and write that buffer to the destination. Here is a very simple and slow example:
    : : [code]
    : : var
    : : source, dest: file of byte;
    : : Buf: byte;
    : : begin
    : : Assign(source, 'sourcefile');
    : : Assign(dest, 'destfile');
    : : Reset(source);
    : : Rewrite(dest);
    : : while not eof(source) do
    : : begin
    : : read(source, Buf);
    : : write(dest, Buf);
    : : end;
    : : Close(source);
    : : Close(dest);
    : : end;
    : : [/code]
    : : You cannot use this method to write a file to CD, because the entire writing process is different. To write a file to CD, you'll probably need to import a COM object to handle to actual burning of the CD, and write the buffer to that object.
    : :
    : Zibadian maybe you have misread something. Our friend wants to write FROM Cd. Also I should note that this "pascal" maybe a Turbo or something similar, where COM objects do not exist.
    :
    : Here is a detection code, this would come in handy:
    : [code]
    : function iscdrom(drive:char):Boolean;
    : var regs:registers;
    : begin
    : cdromfn:=false;
    : if swap(dosversion)<$0200 then exit;
    : drive:=upcase(drive);
    : if(drive<'A') or (drive>'Z') then Exit;
    : fillchar(regs,sizeof(regs),0);
    : regs.cx:=ord(drive)-ord('A');
    : regs.ax:=$150B;
    : intr($2F,regs);
    : iscdrom:=(regs.ax<>0) and (regs.bx=$ADAD);
    : end;
    : [/code]
    :
    : ****************
    : Any questions? Just ask!
    :
    : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    :
    :
    :
    You can still use this way to copy a file from CD to HD or floppy, because the file access is done through the OS and not directly. The user only have to make sure the CD is in the drive.
Sign In or Register to comment.