File input/output: Difference between revisions
Content added Content deleted
(→[[C++]]: simpler version) |
|||
Line 91: | Line 91: | ||
#include <fstream> |
#include <fstream> |
||
#include <string> |
#include <string> |
||
int main() { |
int main() { |
||
string line; |
string line; |
||
Line 115: | Line 115: | ||
return 0; |
return 0; |
||
} |
} |
||
Simpler version: |
|||
#include <iostream> |
|||
#include <istream> |
|||
#include <ostream> |
|||
#include <fstream> |
|||
#include <cstdlib> |
|||
int main() |
|||
{ |
|||
std::ifstream input("input.txt"); |
|||
if (!input) |
|||
{ |
|||
std::cerr << "could not open input.txt for reading.\n"; |
|||
return EXIT_FAILURE; |
|||
} |
|||
std::ofstream output("output.txt"); |
|||
if (!output) |
|||
{ |
|||
std::cerr << "could not open output.txt for writing.\n"; |
|||
return EXIT_FAILURE; |
|||
} |
|||
output << input.rdbuf(); |
|||
if (!output) |
|||
{ |
|||
std::cerr << "error copying the data.\n"; |
|||
return EXIT_FAILURE; |
|||
} |
|||
return EXIT_SUCCESS; |
|||
} |
|||
==[[Java]]== |
==[[Java]]== |
Revision as of 21:30, 21 January 2007
File input/output
You are encouraged to solve this task according to the task description, using any language you may know.
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".
Ada
Compiler: GCC 4.1.2
with Ada.Text_IO; use Ada.Text_IO; procedure File_IO is Input, Output : File_Type; Line : String (1 .. 10_000); Last : Natural; begin Create (Output, Out_File, "output.txt"); Open (Input, In_File, "input.txt"); while not End_Of_File (Input) loop Get_Line (Input, Line, Last); Put_Line (Output, Line (1 .. Last)); end loop; Close (Input); Close (Output); end File_IO;
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; }
Simpler version:
#include <iostream> #include <istream> #include <ostream> #include <fstream> #include <cstdlib>
int main() { std::ifstream input("input.txt"); if (!input) { std::cerr << "could not open input.txt for reading.\n"; return EXIT_FAILURE; }
std::ofstream output("output.txt"); if (!output) { std::cerr << "could not open output.txt for writing.\n"; return EXIT_FAILURE; }
output << input.rdbuf(); if (!output) { std::cerr << "error copying the data.\n"; return EXIT_FAILURE; }
return EXIT_SUCCESS; }
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"; # One '>' overwrites file, Two '>>' appends to file. 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_contents('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()
Ruby
Interpreter: Ruby 1.8.4
begin of = File.new("output.txt", "w") File.open("input.txt").each_line do | line | of.puts line end rescue Exception => e $stderr.puts "Exception raised: #{e}" ensure of.close end
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
#!/bin/sh # another way, using the 'cat' program cat input.txt >output.txt
# yet another way, using the 'cp' utility cp input.txt output.txt