How to perform input and output

ru en

Programming languages:

C ^

Read from file input.txt

#include <stdio.h>

int main() {
  int a, b;
  char p[100], q[100];
  FILE *fin = fopen("input.txt", "rt");
  fscanf(fin, "%d%d", &a, &b);
  fgets(p, 100, fin);
  fgets(q, 100, fin);
  return 0;
}

Read from standard input

#include <stdio.h>

int main() {
  int a, b;
  char p[100], q[100];
  scanf("%d%d", &a, &b);
  fgets(p, 100, stdin);
  fgets(q, 100, stdin);
  return 0;
}

Write to file output.txt

#include <stdio.h>

int main() {
  int a, b;
  char p[100], q[100];
  FILE *fout = fopen("output.txt", "wt");
  fprintf(fout, "%d %d", a, b);
  fprintf(fout, "%s\n%s\n", p, q);
  return 0;
}

Write to standard output

#include <stdio.h>

int main() {
  int a, b;
  char p[100], q[100];
  printf("%d %d", a, b);
  printf("%s\n%s\n", p, q);
  return 0;
}

C# ^

Read from file input.txt

using System.IO;

class Program {
    static void Main() {
        int a, b;
        string p, q;
        StreamReader reader = new StreamReader("input.txt");
        string[] tokens = reader.ReadLine().Split(' ');
        a = int.Parse(tokens[0]);
        b = int.Parse(tokens[1]);
        p = reader.ReadLine();
        q = reader.ReadLine();
    }
}

Read from standard input

using System;

class Program {
    static void Main() {
        int a, b;
        string p, q;
        string[] tokens = Console.ReadLine().Split(' ');
        a = int.Parse(tokens[0]);
        b = int.Parse(tokens[1]);
        p = Console.ReadLine();
        q = Console.ReadLine();
    }
}

Write to file output.txt

using System.IO;

class Program
{
    static void Main() {
        int a = 1, b = 1;
        string p = "zzz", q = "zzz";
        StreamWriter writer = new StreamWriter("output.txt");
        writer.WriteLine("{0} {1}", a, b);
        writer.WriteLine(p);
        writer.WriteLine(q);
        writer.Flush();
    }
}

Write to standard output

using System;

class Program
{
    static void Main() {
        int a = 1, b = 1;
        string p = "zzz", q = "zzz";
        Console.WriteLine("{0} {1}", a, b);
        Console.WriteLine(p);
        Console.WriteLine(q);
    }
}

C++ ^

Read from file input.txt

#include <fstream>
#include <string>
int main() {
  std::ifstream fin("input.txt");
  int a, b;
  std::string p, q;
  fin >> a >> b;
  std::getline(fin, p);
  std::getline(fin, q);
  return 0;
}

Read from standard input

#include <iostream>
#include <string>
int main() {
  int a, b;
  std::string p, q;
  std::cin >> a >> b;
  std::getline(std::cin, p);
  std::getline(std::cin, q);
  return 0;
}

Write to file output.txt

#include <fstream>
#include <string>
int main() {
  std::ofstream fout("output.txt");
  int a, b;
  std::string p, q;
  fout << a << " " << b;
  fout << p << std::endl << q << std::endl;
  return 0;
}

Write to standard output

#include <iostream>
#include <string>
int main() {
  int a, b;
  std::string p, q;
  std::cout << a << " " << b;
  std::cout << p << std::endl << q << std::endl;
  return 0;
}

Read interactive query and write response

#include <iostream>
#include <string>
int main() {
  std::string p, q;
  std::cin >> p;
  std::cout << q << "\n";
  std::cout.flush();
  return 0;
}

Free Basic ^

Read from file input.txt

dim a as integer, b as integer
dim p as string, q as string
open "input.txt" for input as #1
input #1, a, b
line input #1, p
line input #1, q

Read from standard input

dim a as integer, b as integer
dim p as string, q as string
input a, b
line input p
line input q

Write to file output.txt

dim a as integer, b as integer
dim p as string, q as string
open "output.txt" for output as #2
print #2, a; b
print #2, p
print #2, q

Write to standard output

dim a as integer, b as integer
dim p as string, q as string
print a; b
print p
print q

Free Pascal ^

Read from file input.txt

{$mode objfpc}
var
  fin: TextFile;
  a, b: Integer;
  p, q: String;
begin
  AssignFile(fin, 'input.txt'); Reset(fin);
  Read(fin, a, b);
  Readln(fin, p);
  Readln(fin, q);
end.

Read from standard input

{$mode objfpc}
var
  a, b: Integer;
  p, q: String;
begin
  Read(a, b);
  Readln(p);
  Readln(q);
end.

Write to file output.txt

var
  fout: TextFile;
  a, b: Integer;
  p, q: String;
begin
  AssignFile(fin, 'output.txt'); Rewrite(fout);
  Write(fout, a, ' ', b);
  Writeln(fout, p);
  Writeln(fout, q);
end;

Write to standard output

var
  a, b: Integer;
  p, q: String;
begin
  Write(a, ' ', b);
  Writeln(p);
  Writeln(q);
end;

Read interactive query and write response

var
  p, q: String;
begin
  Readln(p);
  Writeln(q);
  Flush(output);
end;

Java ^

Read from file input.txt

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class test {
  public static void main(String args[]) throws FileNotFoundException {
    int a, b;
    String p, q;
    Scanner scanner = new Scanner(new File("input.txt"));
    a = scanner.nextInt();
    b = scanner.nextInt();
    p = scanner.nextLine();
    q = scanner.nextLine();
  }
}

Read from standard input

import java.io.FileNotFoundException;
import java.util.Scanner;

public class test {
  public static void main(String args[]) throws FileNotFoundException {
    int a, b;
    String p, q;
    Scanner scanner = new Scanner(System.in);
    a = scanner.nextInt();
    b = scanner.nextInt();
    p = scanner.nextLine();
    q = scanner.nextLine();
  }
}

Write to file output.txt

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class test {
  public static void main(String args[]) throws FileNotFoundException {
    int a = 1, b = 1;
    String p = "zzz", q = "zzz";
    PrintWriter writer = new PrintWriter("output.txt");
    writer.printf("%d %d", a, b);
    writer.println(p);
    writer.println(q);
    writer.flush();
  }
}

Write to standard output

public class test {
  public static void main(String args[]) {
    int a = 1, b = 1;
    String p = "zzz", q = "zzz";
    System.out.printf("%d %d", a, b);
    System.out.println(p);
    System.out.println(q);
  }
}

NodeJS ^

Read from file input.txt

var file = require('fs');
var input = file.readFileSync('input.txt', 'utf8');
var ints = input.split(/\s+/).map(x => 1 * x); // numbers
var a = ints[0], b = ints[1];
var strs = input.split(/[\n\r]+/); // strings
var p = strs[0], q = strs[1]

Read from standard input

var file = require('fs');
var input = file.readFileSync(process.stdin.fd, 'utf8');
var ints = input.split(/\s+/).map(x => 1 * x); // numbers
var a = ints[0], b = ints[1];
var strs = input.split(/[\n\r]+/); // strings
var p = strs[0], q = strs[1]

Write to file output.txt

var file = require('fs');
file.writeFileSync('output.txt', a + ' ' + b);
file.writeFileSync('output.txt', p + '\n' + q);

Write to standard output

console.log(a, b);
console.log(p);
console.log(q);

Perl ^

Read from file input.txt

use strict;
use warnings;

open my $fin, '<input.txt';
my ($a, $b) = map 0 + $_, do { local $/ = undef; split /\s+/, <$fin>; }; # numbers
my ($p, $q) = <$fin>; # strings

Read from standard input

use strict;
use warnings;

my ($a, $b) = map 0 + $_, do { local $/ = undef; split /\s+/, <>; }; # numbers
my ($p, $q) = <>; # strings

Write to file output.txt

use strict;
use warnings;

my ($a, $b) = (1, 1);
my ($p, $q) = ('zzz') x 2;
open my $fout, '>output.txt';
print $fout "$a $b";
print $fout "$p\n$q\n";

Write to standard output

use strict;
use warnings;

my ($a, $b) = (1, 1);
my ($p, $q) = ('zzz') x 2;
print "$a $b";
print "$p\n$q\n";

Python ^

Read from file input.txt

with open('input.txt', 'r') as fin:
  a, b = map(int, fin.read().split()) # numbers
  p, q = fin.readlines() # strings

Read from standard input

import sys
a, b = map(int, sys.stdin.read().split()) # numbers
p, q = sys.stdin.readlines() # strings

Write to file output.txt

a = b = 1
p = q = 'zzz'
with open('output.txt', 'w') as fout:
  print(a, b, file=fout)
  print(p, file=fout)
  print(q, file=fout)

Write to standard output

a = b = 1
p = q = 'zzz'
print(a, b)
print(p)
print(q)

R ^

Read from file input.txt

input <- file("input.txt", "r")
ab <- scan(input, nlines=1, quiet=TRUE) # numbers
pq <- readLines(input, n=2, warn=FALSE)) # strings

Read from standard input

input <- file("stdin", "r")
ab <- scan(input, nlines=1, quiet=TRUE) # numbers
pq <- readLines(input, n=2, warn=FALSE)) # strings

Write to file output.txt

a <- b <- 1
p <- q <- "zzz"
fout <- file("output.txt", "w")
cat(a, b, file=fout)
cat(p, q, file=fout, sep="\n")

Write to standard output

a <- b <- 1
p <- q <- "zzz"
cat(a, b)
cat(p, q, sep="\n")

Rust ^

Read from file input.txt

use std::io::*;

fn main() -> Result<()> {
    let mut buffer = String::new();
    let f = std::fs::File::open("input.txt")?;
    BufReader::new(f).read_line(&mut buffer)?;
    let n: Vec<_> = buffer.split_whitespace().map(|x| x.parse::<i32>().unwrap()).collect();
    Ok(())
}

Read from standard input

use std::io::*;

fn main() -> Result<()> {
    let mut buffer = String::new();
    stdin().read_line(&mut buffer)?;
    let n: Vec<_> = buffer.split_whitespace().map(|x| x.parse::<i32>().unwrap()).collect();
    Ok(())
}

Write to file output.txt

use std::io::*;

fn main() -> Result<()> {
    let mut f = BufWriter::new(std::fs::File::create("output.txt")?);
    let a = 1; let b = 1;
    let p = String::from("zzz");
    write!(f, "{} {}", a, b)?;
    write!(f, "{}", p)?;
    Ok(())
}

Write to standard output

fn main() -> std::io::Result<()> {
    let a = 1; let b = 1;
    let p = String::from("zzz");
    println!("{} {}", a, b);
    println!("{}", p);
    Ok(())
}
Far Eastern Federal University