Reading/Writing to a file... I don't seem to understand...

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

[code]

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.

[/code]

Comments

  • : 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
    :
    : [code]
    :
    : 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.
    :
    : [/code]
    :
    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.
    [code]
    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;
    [/code]

  • 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
    : :
    : : [code]
    : :
    : : 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.
    : :
    : : [/code]
    : :
    : 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.
    : [code]
    : 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;
    : [/code]
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

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

Categories