Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Reading/Writing to a file... I don't seem to understand... Posted by jim_giboney on 23 Feb 2003 at 12:47 PM
I am just not really understanding this code. Apparently it creates a file, called file2.txt (which is SO cool). But what is Filein, Fileout, and Mychar? What do the do? I can see that MYCHAR goes to FILEOUT and is written to FILE2.TXT, but what does FILEIN in do? Is there a way to read input from a user and create a file? Can someone just explain the code to me? Like comment it and show what it does?

Thanks! All help is appreciated!
-JIM


   program CopyOneByteFile;

   var
      mychar : char;
      filein, fileout : text;

   begin
      assign (filein, 'c:\file1.txt');
      reset (filein);
      assign (fileout, 'c:\file2.txt');
      rewrite (fileout);
      read (filein, mychar);
      write (fileout, mychar);
      close(filein);
      close(fileout)
   end.



Report
Re: Reading/Writing to a file... I don't seem to understand... Posted by zibadian on 23 Feb 2003 at 1:09 PM
: I am just not really understanding this code. Apparently it creates a file, called file2.txt (which is SO cool). But what is Filein, Fileout, and Mychar? What do the do? I can see that MYCHAR goes to FILEOUT and is written to FILE2.TXT, but what does FILEIN in do? Is there a way to read input from a user and create a file? Can someone just explain the code to me? Like comment it and show what it does?
:
: Thanks! All help is appreciated!
: -JIM
:
:
: 
:    program CopyOneByteFile;
: 
:    var
:       mychar : char; 
:       filein, fileout : text;
: 
:    begin
:       assign (filein, 'c:\file1.txt');
:       reset (filein); { place file-cursor to first character }
:       assign (fileout, 'c:\file2.txt');
:       rewrite (fileout); { create/recreate the file "file2" }
:       read (filein, mychar); { read the first character of "file1.txt" }
:       write (fileout, mychar); { write the character to "file2.txt" }
:       close(filein); { close both files }
:       close(fileout)
:    end.
: 
: 

:
This code copies the first character in the file1.txt to file2.txt. FileIn is a text-file variable, which in this case is opened to be read from. The FileOut is also a text-variable, which is opened to write to. (both times see help on Assign). Here is a similar code which allows the user to type a single line and saves it to a file.
var
  line: string;
  userfile: text;
begin
  Write('Type something: ');
  Readln(line); { get the user input }
  Assign(userfile, 'myfile.txt');
  Rewrite(userfile); { create "myfile.txt" for writing }
  Writeln(userfile, line); { write the user input to the file }
  Close(userfile); { close the file }
end;


Report
Thanks a lot Posted by jim_giboney on 23 Feb 2003 at 1:48 PM
THANKS!


: : I am just not really understanding this code. Apparently it creates a file, called file2.txt (which is SO cool). But what is Filein, Fileout, and Mychar? What do the do? I can see that MYCHAR goes to FILEOUT and is written to FILE2.TXT, but what does FILEIN in do? Is there a way to read input from a user and create a file? Can someone just explain the code to me? Like comment it and show what it does?
: :
: : Thanks! All help is appreciated!
: : -JIM
: :
: :
: : 
: :    program CopyOneByteFile;
: : 
: :    var
: :       mychar : char; 
: :       filein, fileout : text;
: : 
: :    begin
: :       assign (filein, 'c:\file1.txt');
: :       reset (filein); { place file-cursor to first character }
: :       assign (fileout, 'c:\file2.txt');
: :       rewrite (fileout); { create/recreate the file "file2" }
: :       read (filein, mychar); { read the first character of "file1.txt" }
: :       write (fileout, mychar); { write the character to "file2.txt" }
: :       close(filein); { close both files }
: :       close(fileout)
: :    end.
: : 
: : 

: :
: This code copies the first character in the file1.txt to file2.txt. FileIn is a text-file variable, which in this case is opened to be read from. The FileOut is also a text-variable, which is opened to write to. (both times see help on Assign). Here is a similar code which allows the user to type a single line and saves it to a file.
:
: var
:   line: string;
:   userfile: text;
: begin
:   Write('Type something: ');
:   Readln(line); { get the user input }
:   Assign(userfile, 'myfile.txt');
:   Rewrite(userfile); { create "myfile.txt" for writing }
:   Writeln(userfile, line); { write the user input to the file }
:   Close(userfile); { close the file }
: end;
: 

:
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 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.
Operated by CommunityHeaven, a BootstrapLabs company.