File input/output: Difference between revisions
Content added Content deleted
(Added a second version for Java, this time closing both files) |
|||
Line 215: | Line 215: | ||
outputFile.writelines(inputFile) |
outputFile.writelines(inputFile) |
||
finally: |
finally: |
||
outputFile.close() |
|||
finally: |
finally: |
||
inputFile.close() |
|||
==[[TCL]]== |
==[[TCL]]== |
Revision as of 19:25, 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."); } } } }
C++
Compiler: GCC 3.4.2
#include <iostream> #include <fstream> #include <string> int main() { string line; ifstream input ( "input.txt" ); ofstream output ("output.txt"); if (output.is_open()) { if (input.is_open()) { while (! input.eof() ) { getline (input,line); output << line << endl; } input.close(); } else { cout << "input.txt cannot be opened!\n"; } output.close(); } else { cout << "output.txt cannot be written to!\n"; } return 0; }
Java
Compiler: GCJ 4.1.2
Simple version; files are closed by OS when the program finishes
import java.io.*; public class FileIODemo { public static void main(String[] args) { try { FileInputStream in = new FileInputStream("input.txt"); FileOutputStream out = new FileOutputStream("ouput.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } } catch (Exception e) { e.printStackTrace(); } } }
This version closes both files after without OS intervention
import java.io.*; public class FileIODemo2 { public static void main(String args[]) { InputStream in = null; OutputStream out = null; try { in = new FileInputStream("input.txt"); out = new FileOutputStream("output.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } } catch (Exception e) { System.err.println("Exception while trying to copy: "+e); e.printStackTrace(); // stack trace of place where it happened } finally { // guaranteed to be reached, whatever the exceptions try { if (in != null) in.close(); if (out != null) out.close(); } // close() may also launch exception... yes, Java is like this catch (IOException ioe) { System.err.println("Exception closing files: "+ioe); } } } }
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); ?>
Interpreter: PHP 5
<?php if( $contents = file_get_conents('input.txt') ){ if( !file_put_contents('output.txt') ) echo('could not write output file'); }else{ echo('could not open input file'); } ?>
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: outputFile.close() finally: inputFile.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