File input/output: Difference between revisions
Content added Content deleted
No edit summary |
(Added PHP code) |
||
Line 77: | Line 77: | ||
print OUTPUT $_; |
print OUTPUT $_; |
||
} |
} |
||
==[[PHP]]== |
|||
'''Interpreter:''' [[PHP]] 4 |
|||
<?php |
|||
if (!$in=fopen('input.txt','r')) { |
|||
die('Could not open input.txt!'); |
|||
} |
|||
if (!$out=fopen('output.txt','w')) { |
|||
die('Could not open output.txt!'); |
|||
} |
|||
while(!feof($in)) { |
|||
$data = fread($in,512); |
|||
fwrite($out,$data); |
|||
} |
|||
fclose($out); |
|||
fclose($in); |
|||
?> |
|||
==[[mIRC]]== |
==[[mIRC]]== |
Revision as of 17:56, 21 January 2007
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
In this task, the job is to create a file called "output.txt", and place in it the contents of the file "input.txt".
C
Compiler: GCC 4.1.2
#include <stdio.h> int main(int argc, char **argv) { FILE *in, *out; int c; in = fopen("input.txt", "r"); if (!in) { fprintf(stderr, "Error opening input.txt for reading.\n"); return 1; } out = fopen("output.txt", "w"); if (!out) { fprintf(stderr, "Error opening output.txt for writing.\n"); return 1; } while ((c = fgetc(in)) != EOF) { fputc(c, out); } return 0; }
C#
Platform: .NET Language Version: 1.0+
using System; using System.IO; namespace FileIO { class Program { static void Main(string[] args) { if (File.Exists("input.txt")) { TextReader tr = File.OpenText("input.txt"); TextWriter tw = new StreamWriter(File.OpenWrite("output.txt")); while (tr.Peek() != -1) { string line = tr.ReadLine(); tw.WriteLine(line); } tw.Close(); tr.Close(); } else { Console.WriteLine("Input File Missing."); } } } }
Perl
Interpreter: Perl 5.8.8
#!/usr/bin/perl -w open INPUT, "<input.txt"; open OUTPUT, ">output.txt"; while ( <INPUT> ) { print OUTPUT $_; }
PHP
Interpreter: PHP 4
<?php if (!$in=fopen('input.txt','r')) { die('Could not open input.txt!'); } if (!$out=fopen('output.txt','w')) { die('Could not open output.txt!'); } while(!feof($in)) { $data = fread($in,512); fwrite($out,$data); } fclose($out); fclose($in); ?>
mIRC
Compiler: mIRC
alias Write2FileAndReadIt { .write myfilename.txt Goodbye Mike! .echo -a Myfilename.txt contains: $read(myfilename.txt,1) }
Python
Interpreter: Python 2.4
In short form:
open("output.txt", "w").writelines(open("input.txt"))
With proper closing and exception-handling:
inputFile = open("input.txt","r") try: outputFile = open("output.txt", "w") try: outputFile.writelines(inputFile) finally: inputFile.close() finally: outputFile.close()
TCL
Interpreter: tclsh, eTcl, wish, tixwish
set in [open "input.txt" r] set out [open "output.txt" w] puts -nonewline $out [read $in] close $in close $out
Other File I/O:
#open file for writing set myfile [open "README.TXT" w] #write something to the file puts $myfile "This is line 1, so hello world...." #close the file close $myfile
#open file for reading set myfile [open "README.TXT" r] #read something from the file gets $myfile mydata #show what was read from the file #should print "This is line1, so hello world...." puts $mydata #close the file close $myfile
UNIX Shell
Interpreter: Bourne Shell Operating System: UNIX
#!/bin/sh while read a; do echo "$a" done <input.txt >output.txt