Programmer's Wiki
Advertisement
16px-Pencil

Standard input is the stream where input for a computer program is held. The input is often then stored in a variable.

C[]

#include <stdio.h>
 
#define MAX_LEN 20
 
char myString[MAX_LEN + 1];
fgets(myString, MAX_LEN + 1, stdin);

C++[]

#include <iostream>
#include <string>

std::string myString;
std::cin >> myString;

C#[]

string MyString = "";
MyString = Console.ReadLine();
Console.WriteLine(MyString);

VB.NET[]

Dim MyString As String = ""
MyString = Console.ReadLine()
Console.WriteLine(MyString)

Haskell[]

do myString <- getLine
   -- do stuff here

Java[]

Old version using BufferedReader[]

import java.io.*;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String myString = br.readLine();


Newer version using Scanner[]

import java.util.Scanner;
Scanner scan = new Scanner(System.in);
String operat = scan.next(); //Change "String" to correct type.

Here used with int:

import java.util.Scanner;
Scanner scan = new Scanner(System.in);
int operat = scan.nextInt(); //Notice how "scan.next();" changed to "scan.nextInt();"

OCaml[]

let my_string = read_line () in
  (* do stuff here *)

Perl[]

my $myString = <STDIN>;

PHP[]

$myString = fgets(STDIN);

Or:

$stdin = fopen('php://stdin', 'r'); // opens standard input
$line = fgets($stdin); // reads until user presses ENTER

Python[]

2.x

myString = raw_input()

3.x

myString = input()

Ruby[]

myString = gets

STDIN.gets

External Links[]

See Also[]

Advertisement