WeixiangYan/CodeTransOcean huggingface dataset:
Task: Quine







Python: w = "print('w = ' + chr(34) + w + chr(34) + chr(10) + w)"
print('w = ' + chr(34) + w + chr(34) + chr(10) + w)


VB: Public Sub quine()
    quote = Chr(34)
    comma = Chr(44)
    cont = Chr(32) & Chr(95)
    n = Array( _
"Public Sub quine()", _
"    quote = Chr(34)", _
"    comma = Chr(44)", _
"    cont = Chr(32) & Chr(95)", _
"    n = Array( _", _
"    For i = 0 To 4", _
"        Debug.Print n(i)", _
"    Next i", _
"    For i = 0 To 15", _
"        Debug.Print quote & n(i) & quote & comma & cont", _
"    Next i", _
"    Debug.Print quote & n(15) & quote & Chr(41)", _
"    For i = 5 To 15", _
"        Debug.Print n(i)", _
"    Next i", _
"End Sub")
    For i = 0 To 4
        Debug.Print n(i)
    Next i
    For i = 0 To 14
        Debug.Print quote & n(i) & quote & comma & cont
    Next i
    Debug.Print quote & n(15) & quote & Chr(41)
    For i = 5 To 15
        Debug.Print n(i)
    Next i
End Sub
Task: Quine

C: #include <stdio.h>

static char sym[] = "\n\t\\\"";

int main(void) {
	const char *code = "#include <stdio.h>%c%cstatic char sym[] = %c%cn%ct%c%c%c%c%c;%c%cint main(void) {%c%cconst char *code = %c%s%c;%c%cprintf(code, sym[0], sym[0], sym[3], sym[2], sym[2], sym[2], sym[2], sym[2], sym[3], sym[3], sym[0], sym[0], sym[0], sym[1], sym[3], code, sym[3], sym[0], sym[1], sym[0], sym[0], sym[1], sym[0], sym[0]);%c%c%creturn 0;%c}%c";
	printf(code, sym[0], sym[0], sym[3], sym[2], sym[2], sym[2], sym[2], sym[2], sym[3], sym[3], sym[0], sym[0], sym[0], sym[1], sym[3], code, sym[3], sym[0], sym[1], sym[0], sym[0], sym[1], sym[0], sym[0]);

	return 0;
}




Go: package main

import "fmt"

func main() {
	a := "package main\n\nimport \"fmt\"\n\nfunc main() {\n\ta := %q\n\tfmt.Printf(a, a)\n}\n"
	fmt.Printf(a, a)
}
Task: Quine





Java: module test
    {
    @Inject Console console;
    void run()
        {
        console.print($./test.x);
        }
    }



Python: w = "print('w = ' + chr(34) + w + chr(34) + chr(10) + w)"
print('w = ' + chr(34) + w + chr(34) + chr(10) + w)
Task: Quine

C: #include <stdio.h>

static char sym[] = "\n\t\\\"";

int main(void) {
	const char *code = "#include <stdio.h>%c%cstatic char sym[] = %c%cn%ct%c%c%c%c%c;%c%cint main(void) {%c%cconst char *code = %c%s%c;%c%cprintf(code, sym[0], sym[0], sym[3], sym[2], sym[2], sym[2], sym[2], sym[2], sym[3], sym[3], sym[0], sym[0], sym[0], sym[1], sym[3], code, sym[3], sym[0], sym[1], sym[0], sym[0], sym[1], sym[0], sym[0]);%c%c%creturn 0;%c}%c";
	printf(code, sym[0], sym[0], sym[3], sym[2], sym[2], sym[2], sym[2], sym[2], sym[3], sym[3], sym[0], sym[0], sym[0], sym[1], sym[3], code, sym[3], sym[0], sym[1], sym[0], sym[0], sym[1], sym[0], sym[0]);

	return 0;
}






PHP: <?php $p = '<?php $p = %c%s%c; printf($p,39,$p,39); ?>
'; printf($p,39,$p,39); ?>
Task: Quine



C#: class Program { static void Main() { var s = "class Program {{ static void Main() {{ var s = {0}{1}{0}; System.Console.WriteLine(s, (char)34, s); }} }}"; System.Console.WriteLine(s, (char)34, s); } }




PHP: <?php $p = '<?php $p = %c%s%c; printf($p,39,$p,39); ?>
'; printf($p,39,$p,39); ?>
Task: Quine


C++: #include<cstdio>
int main(){char n[]=R"(#include<cstdio>
int main(){char n[]=R"(%s%c";printf(n,n,41);})";printf(n,n,41);}





PHP: <?php $p = '<?php $p = %c%s%c; printf($p,39,$p,39); ?>
'; printf($p,39,$p,39); ?>
Task: Quine




Go: package main

import "fmt"

func main() {
	a := "package main\n\nimport \"fmt\"\n\nfunc main() {\n\ta := %q\n\tfmt.Printf(a, a)\n}\n"
	fmt.Printf(a, a)
}



PHP: <?php $p = '<?php $p = %c%s%c; printf($p,39,$p,39); ?>
'; printf($p,39,$p,39); ?>
Task: Quine





Java: module test
    {
    @Inject Console console;
    void run()
        {
        console.print($./test.x);
        }
    }


PHP: <?php $p = '<?php $p = %c%s%c; printf($p,39,$p,39); ?>
'; printf($p,39,$p,39); ?>
Task: Quine






PHP: <?php $p = '<?php $p = %c%s%c; printf($p,39,$p,39); ?>
'; printf($p,39,$p,39); ?>


Python: w = "print('w = ' + chr(34) + w + chr(34) + chr(10) + w)"
print('w = ' + chr(34) + w + chr(34) + chr(10) + w)
Task: Quine






PHP: <?php $p = '<?php $p = %c%s%c; printf($p,39,$p,39); ?>
'; printf($p,39,$p,39); ?>



VB: Public Sub quine()
    quote = Chr(34)
    comma = Chr(44)
    cont = Chr(32) & Chr(95)
    n = Array( _
"Public Sub quine()", _
"    quote = Chr(34)", _
"    comma = Chr(44)", _
"    cont = Chr(32) & Chr(95)", _
"    n = Array( _", _
"    For i = 0 To 4", _
"        Debug.Print n(i)", _
"    Next i", _
"    For i = 0 To 15", _
"        Debug.Print quote & n(i) & quote & comma & cont", _
"    Next i", _
"    Debug.Print quote & n(15) & quote & Chr(41)", _
"    For i = 5 To 15", _
"        Debug.Print n(i)", _
"    Next i", _
"End Sub")
    For i = 0 To 4
        Debug.Print n(i)
    Next i
    For i = 0 To 14
        Debug.Print quote & n(i) & quote & comma & cont
    Next i
    Debug.Print quote & n(15) & quote & Chr(41)
    For i = 5 To 15
        Debug.Print n(i)
    Next i
End Sub
Task: Sequence of primes by trial division

C: #include<stdio.h>

int isPrime(unsigned int n)
{
	unsigned int num;
	
	if ( n < 2||!(n & 1)) 
		return n == 2;

	for (num = 3; num <= n/num; num += 2)
		if (!(n % num)) 
			return 0;
	return 1;
}

int main()
{
	unsigned int l,u,i,sum=0;
	
	printf("Enter lower and upper bounds: ");
	scanf("%ld%ld",&l,&u);
	
	for(i=l;i<=u;i++){
		if(isPrime(i)==1)
			{
				printf("\n%ld",i);
				sum++;
			}
	}
	
	printf("\n\nPrime numbers found in [%ld,%ld] : %ld",l,u,sum);
	
	return 0;
}




Go: package main
 
import "fmt"
 
func NumsFromBy(from int, by int, ch chan<- int) {
  for i := from; ; i+=by {
    ch <- i
  }
}
 
func Filter(in <-chan int, out chan<- int, prime int) {
  for {
    i := <-in
    if i%prime != 0 {            
      out <- i
    }
  }
}
 
func Sieve(out chan<- int) { 
  out <- 3
  q := 9
  ps := make(chan int)
  go Sieve(ps)                   
  p := <-ps         
  nums := make(chan int)
  go NumsFromBy(5,2,nums)        
  for i := 0; ; i++ {
    n := <-nums
    if n < q {
    	out <- n                 
    } else {
        ch1 := make(chan int)    
        go Filter(nums, ch1, p)  
        nums = ch1
    	p = <-ps                 
    	q = p*p                  
    }
  }
}

func primes (c chan<- int) {
  c <- 2
  go Sieve(c)
}
 
func main() {
  ch := make(chan int)
  go primes(ch)
  fmt.Print("First twenty:")
  for i := 0; i < 20; i++ {
    fmt.Print(" ", <-ch)
  }
  fmt.Println()
}
Task: Sequence of primes by trial division

C: #include<stdio.h>

int isPrime(unsigned int n)
{
	unsigned int num;
	
	if ( n < 2||!(n & 1)) 
		return n == 2;

	for (num = 3; num <= n/num; num += 2)
		if (!(n % num)) 
			return 0;
	return 1;
}

int main()
{
	unsigned int l,u,i,sum=0;
	
	printf("Enter lower and upper bounds: ");
	scanf("%ld%ld",&l,&u);
	
	for(i=l;i<=u;i++){
		if(isPrime(i)==1)
			{
				printf("\n%ld",i);
				sum++;
			}
	}
	
	printf("\n\nPrime numbers found in [%ld,%ld] : %ld",l,u,sum);
	
	return 0;
}





Java: import java.util.stream.IntStream;

public class Test {

    static IntStream getPrimes(int start, int end) {
        return IntStream.rangeClosed(start, end).filter(n -> isPrime(n));
    }

    public static boolean isPrime(long x) {
        if (x < 3 || x % 2 == 0)
            return x == 2;

        long max = (long) Math.sqrt(x);
        for (long n = 3; n <= max; n += 2) {
            if (x % n == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        getPrimes(0, 100).forEach(p -> System.out.printf("%d, ", p));
    }
}
Task: Sequence of primes by trial division



C#: using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    static void Main() {
        Console.WriteLine(string.Join(" ", Primes(100)));
    }

    static IEnumerable<int> Primes(int limit) => Enumerable.Range(2, limit-1).Where(IsPrime);
    static bool IsPrime(int n) => Enumerable.Range(2, (int)Math.Sqrt(n)-1).All(i => n % i != 0);
}


Go: package main
 
import "fmt"
 
func NumsFromBy(from int, by int, ch chan<- int) {
  for i := from; ; i+=by {
    ch <- i
  }
}
 
func Filter(in <-chan int, out chan<- int, prime int) {
  for {
    i := <-in
    if i%prime != 0 {            
      out <- i
    }
  }
}
 
func Sieve(out chan<- int) { 
  out <- 3
  q := 9
  ps := make(chan int)
  go Sieve(ps)                   
  p := <-ps         
  nums := make(chan int)
  go NumsFromBy(5,2,nums)        
  for i := 0; ; i++ {
    n := <-nums
    if n < q {
    	out <- n                 
    } else {
        ch1 := make(chan int)    
        go Filter(nums, ch1, p)  
        nums = ch1
    	p = <-ps                 
    	q = p*p                  
    }
  }
}

func primes (c chan<- int) {
  c <- 2
  go Sieve(c)
}
 
func main() {
  ch := make(chan int)
  go primes(ch)
  fmt.Print("First twenty:")
  for i := 0; i < 20; i++ {
    fmt.Print(" ", <-ch)
  }
  fmt.Println()
}
Task: Sequence of primes by trial division



C#: using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    static void Main() {
        Console.WriteLine(string.Join(" ", Primes(100)));
    }

    static IEnumerable<int> Primes(int limit) => Enumerable.Range(2, limit-1).Where(IsPrime);
    static bool IsPrime(int n) => Enumerable.Range(2, (int)Math.Sqrt(n)-1).All(i => n % i != 0);
}





Python: def prime(a):
    return not (a < 2 or any(a % x == 0 for x in xrange(2, int(a**0.5) + 1)))

def primes_below(n):
    return [i for i in range(n) if prime(i)]
Task: Sequence of primes by trial division




Go: package main
 
import "fmt"
 
func NumsFromBy(from int, by int, ch chan<- int) {
  for i := from; ; i+=by {
    ch <- i
  }
}
 
func Filter(in <-chan int, out chan<- int, prime int) {
  for {
    i := <-in
    if i%prime != 0 {            
      out <- i
    }
  }
}
 
func Sieve(out chan<- int) { 
  out <- 3
  q := 9
  ps := make(chan int)
  go Sieve(ps)                   
  p := <-ps         
  nums := make(chan int)
  go NumsFromBy(5,2,nums)        
  for i := 0; ; i++ {
    n := <-nums
    if n < q {
    	out <- n                 
    } else {
        ch1 := make(chan int)    
        go Filter(nums, ch1, p)  
        nums = ch1
    	p = <-ps                 
    	q = p*p                  
    }
  }
}

func primes (c chan<- int) {
  c <- 2
  go Sieve(c)
}
 
func main() {
  ch := make(chan int)
  go primes(ch)
  fmt.Print("First twenty:")
  for i := 0; i < 20; i++ {
    fmt.Print(" ", <-ch)
  }
  fmt.Println()
}


Java: import java.util.stream.IntStream;

public class Test {

    static IntStream getPrimes(int start, int end) {
        return IntStream.rangeClosed(start, end).filter(n -> isPrime(n));
    }

    public static boolean isPrime(long x) {
        if (x < 3 || x % 2 == 0)
            return x == 2;

        long max = (long) Math.sqrt(x);
        for (long n = 3; n <= max; n += 2) {
            if (x % n == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        getPrimes(0, 100).forEach(p -> System.out.printf("%d, ", p));
    }
}
Task: Sequence of primes by trial division




Go: package main
 
import "fmt"
 
func NumsFromBy(from int, by int, ch chan<- int) {
  for i := from; ; i+=by {
    ch <- i
  }
}
 
func Filter(in <-chan int, out chan<- int, prime int) {
  for {
    i := <-in
    if i%prime != 0 {            
      out <- i
    }
  }
}
 
func Sieve(out chan<- int) { 
  out <- 3
  q := 9
  ps := make(chan int)
  go Sieve(ps)                   
  p := <-ps         
  nums := make(chan int)
  go NumsFromBy(5,2,nums)        
  for i := 0; ; i++ {
    n := <-nums
    if n < q {
    	out <- n                 
    } else {
        ch1 := make(chan int)    
        go Filter(nums, ch1, p)  
        nums = ch1
    	p = <-ps                 
    	q = p*p                  
    }
  }
}

func primes (c chan<- int) {
  c <- 2
  go Sieve(c)
}
 
func main() {
  ch := make(chan int)
  go primes(ch)
  fmt.Print("First twenty:")
  for i := 0; i < 20; i++ {
    fmt.Print(" ", <-ch)
  }
  fmt.Println()
}




Python: def prime(a):
    return not (a < 2 or any(a % x == 0 for x in xrange(2, int(a**0.5) + 1)))

def primes_below(n):
    return [i for i in range(n) if prime(i)]
Task: Sequence of primes by trial division

C: #include<stdio.h>

int isPrime(unsigned int n)
{
	unsigned int num;
	
	if ( n < 2||!(n & 1)) 
		return n == 2;

	for (num = 3; num <= n/num; num += 2)
		if (!(n % num)) 
			return 0;
	return 1;
}

int main()
{
	unsigned int l,u,i,sum=0;
	
	printf("Enter lower and upper bounds: ");
	scanf("%ld%ld",&l,&u);
	
	for(i=l;i<=u;i++){
		if(isPrime(i)==1)
			{
				printf("\n%ld",i);
				sum++;
			}
	}
	
	printf("\n\nPrime numbers found in [%ld,%ld] : %ld",l,u,sum);
	
	return 0;
}



C#: using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    static void Main() {
        Console.WriteLine(string.Join(" ", Primes(100)));
    }

    static IEnumerable<int> Primes(int limit) => Enumerable.Range(2, limit-1).Where(IsPrime);
    static bool IsPrime(int n) => Enumerable.Range(2, (int)Math.Sqrt(n)-1).All(i => n % i != 0);
}
Task: Sequence of primes by trial division



C#: using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    static void Main() {
        Console.WriteLine(string.Join(" ", Primes(100)));
    }

    static IEnumerable<int> Primes(int limit) => Enumerable.Range(2, limit-1).Where(IsPrime);
    static bool IsPrime(int n) => Enumerable.Range(2, (int)Math.Sqrt(n)-1).All(i => n % i != 0);
}



Java: import java.util.stream.IntStream;

public class Test {

    static IntStream getPrimes(int start, int end) {
        return IntStream.rangeClosed(start, end).filter(n -> isPrime(n));
    }

    public static boolean isPrime(long x) {
        if (x < 3 || x % 2 == 0)
            return x == 2;

        long max = (long) Math.sqrt(x);
        for (long n = 3; n <= max; n += 2) {
            if (x % n == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        getPrimes(0, 100).forEach(p -> System.out.printf("%d, ", p));
    }
}
Task: Sequence of primes by trial division





Java: import java.util.stream.IntStream;

public class Test {

    static IntStream getPrimes(int start, int end) {
        return IntStream.rangeClosed(start, end).filter(n -> isPrime(n));
    }

    public static boolean isPrime(long x) {
        if (x < 3 || x % 2 == 0)
            return x == 2;

        long max = (long) Math.sqrt(x);
        for (long n = 3; n <= max; n += 2) {
            if (x % n == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        getPrimes(0, 100).forEach(p -> System.out.printf("%d, ", p));
    }
}



Python: def prime(a):
    return not (a < 2 or any(a % x == 0 for x in xrange(2, int(a**0.5) + 1)))

def primes_below(n):
    return [i for i in range(n) if prime(i)]
Task: Sequence of primes by trial division

C: #include<stdio.h>

int isPrime(unsigned int n)
{
	unsigned int num;
	
	if ( n < 2||!(n & 1)) 
		return n == 2;

	for (num = 3; num <= n/num; num += 2)
		if (!(n % num)) 
			return 0;
	return 1;
}

int main()
{
	unsigned int l,u,i,sum=0;
	
	printf("Enter lower and upper bounds: ");
	scanf("%ld%ld",&l,&u);
	
	for(i=l;i<=u;i++){
		if(isPrime(i)==1)
			{
				printf("\n%ld",i);
				sum++;
			}
	}
	
	printf("\n\nPrime numbers found in [%ld,%ld] : %ld",l,u,sum);
	
	return 0;
}







Python: def prime(a):
    return not (a < 2 or any(a % x == 0 for x in xrange(2, int(a**0.5) + 1)))

def primes_below(n):
    return [i for i in range(n) if prime(i)]
Task: Check that file exists

C: #include <sys/types.h> 
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>


int check_reg(const char *path) {
	struct stat sb;
	return stat(path, &sb) == 0 && S_ISREG(sb.st_mode);
}


int check_dir(const char *path) {
	struct stat sb;
	return stat(path, &sb) == 0 && S_ISDIR(sb.st_mode);
}

int main() {
	printf("input.txt is a regular file? %s\n",
	    check_reg("input.txt") ? "yes" : "no");
	printf("docs is a directory? %s\n",
	    check_dir("docs") ? "yes" : "no");
	printf("/input.txt is a regular file? %s\n",
	    check_reg("/input.txt") ? "yes" : "no");
	printf("/docs is a directory? %s\n",
	    check_dir("/docs") ? "yes" : "no");
	return 0;
}


C++: #include "boost/filesystem.hpp"
#include <string>
#include <iostream>

void testfile(std::string name)
{
  boost::filesystem::path file(name);
  if (exists(file))
  {
    if (is_directory(file))
      std::cout << name << " is a directory.\n";
    else
      std::cout << name << " is a non-directory file.\n";
  }
  else
    std::cout << name << " does not exist.\n";
}

int main()
{
  testfile("input.txt");
  testfile("docs");
  testfile("/input.txt");
  testfile("/docs");
}
Task: Check that file exists

C: #include <sys/types.h> 
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>


int check_reg(const char *path) {
	struct stat sb;
	return stat(path, &sb) == 0 && S_ISREG(sb.st_mode);
}


int check_dir(const char *path) {
	struct stat sb;
	return stat(path, &sb) == 0 && S_ISDIR(sb.st_mode);
}

int main() {
	printf("input.txt is a regular file? %s\n",
	    check_reg("input.txt") ? "yes" : "no");
	printf("docs is a directory? %s\n",
	    check_dir("docs") ? "yes" : "no");
	printf("/input.txt is a regular file? %s\n",
	    check_reg("/input.txt") ? "yes" : "no");
	printf("/docs is a directory? %s\n",
	    check_dir("/docs") ? "yes" : "no");
	return 0;
}





Java: import java.io.File;
public class FileExistsTest {
   public static boolean isFileExists(String filename) {
       boolean exists = new File(filename).exists();
       return exists;
   }
   public static void test(String type, String filename) {
       System.out.println("The following " + type + " called " + filename + 
           (isFileExists(filename) ? " exists." : " not exists.")
       );
   }
   public static void main(String args[]) {
        test("file", "input.txt");
        test("file", File.separator + "input.txt");
        test("directory", "docs");
        test("directory", File.separator + "docs" + File.separator);
   }
}
Task: Check that file exists

C: #include <sys/types.h> 
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>


int check_reg(const char *path) {
	struct stat sb;
	return stat(path, &sb) == 0 && S_ISREG(sb.st_mode);
}


int check_dir(const char *path) {
	struct stat sb;
	return stat(path, &sb) == 0 && S_ISDIR(sb.st_mode);
}

int main() {
	printf("input.txt is a regular file? %s\n",
	    check_reg("input.txt") ? "yes" : "no");
	printf("docs is a directory? %s\n",
	    check_dir("docs") ? "yes" : "no");
	printf("/input.txt is a regular file? %s\n",
	    check_reg("/input.txt") ? "yes" : "no");
	printf("/docs is a directory? %s\n",
	    check_dir("/docs") ? "yes" : "no");
	return 0;
}







Python: import os

os.path.isfile("input.txt")
os.path.isfile("/input.txt")
os.path.isdir("docs")
os.path.isdir("/docs")
Task: Check that file exists

C: #include <sys/types.h> 
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>


int check_reg(const char *path) {
	struct stat sb;
	return stat(path, &sb) == 0 && S_ISREG(sb.st_mode);
}


int check_dir(const char *path) {
	struct stat sb;
	return stat(path, &sb) == 0 && S_ISDIR(sb.st_mode);
}

int main() {
	printf("input.txt is a regular file? %s\n",
	    check_reg("input.txt") ? "yes" : "no");
	printf("docs is a directory? %s\n",
	    check_dir("docs") ? "yes" : "no");
	printf("/input.txt is a regular file? %s\n",
	    check_reg("/input.txt") ? "yes" : "no");
	printf("/docs is a directory? %s\n",
	    check_dir("/docs") ? "yes" : "no");
	return 0;
}








VB: Option Explicit

Sub Main_File_Exists()
Dim myFile As String, myDirectory As String
    
    myFile = "Abdu
    myDirectory = "C:\"
    Debug.Print File_Exists(myFile, myDirectory)
End Sub

Function File_Exists(F As String, D As String) As Boolean
    If F = "" Then
        File_Exists = False
    Else
        D = IIf(Right(D, 1) = "\", D, D & "\")
        File_Exists = (Dir(D & F) <> "")
    End If
End Function
Task: Check that file exists


C++: #include "boost/filesystem.hpp"
#include <string>
#include <iostream>

void testfile(std::string name)
{
  boost::filesystem::path file(name);
  if (exists(file))
  {
    if (is_directory(file))
      std::cout << name << " is a directory.\n";
    else
      std::cout << name << " is a non-directory file.\n";
  }
  else
    std::cout << name << " does not exist.\n";
}

int main()
{
  testfile("input.txt");
  testfile("docs");
  testfile("/input.txt");
  testfile("/docs");
}


C#: using System.IO;

Console.WriteLine(File.Exists("input.txt"));
Console.WriteLine(File.Exists("/input.txt"));
Console.WriteLine(Directory.Exists("docs"));
Console.WriteLine(Directory.Exists("/docs"));
Task: Check that file exists


C++: #include "boost/filesystem.hpp"
#include <string>
#include <iostream>

void testfile(std::string name)
{
  boost::filesystem::path file(name);
  if (exists(file))
  {
    if (is_directory(file))
      std::cout << name << " is a directory.\n";
    else
      std::cout << name << " is a non-directory file.\n";
  }
  else
    std::cout << name << " does not exist.\n";
}

int main()
{
  testfile("input.txt");
  testfile("docs");
  testfile("/input.txt");
  testfile("/docs");
}



Go: package main

import (
    "fmt"
    "os"
)

func printStat(p string) {
    switch i, err := os.Stat(p); {
    case err != nil:
        fmt.Println(err)
    case i.IsDir():
        fmt.Println(p, "is a directory")
    default:
        fmt.Println(p, "is a file")
    }
}

func main() {
    printStat("input.txt")
    printStat("/input.txt")
    printStat("docs")
    printStat("/docs")
}
Task: Check that file exists


C++: #include "boost/filesystem.hpp"
#include <string>
#include <iostream>

void testfile(std::string name)
{
  boost::filesystem::path file(name);
  if (exists(file))
  {
    if (is_directory(file))
      std::cout << name << " is a directory.\n";
    else
      std::cout << name << " is a non-directory file.\n";
  }
  else
    std::cout << name << " does not exist.\n";
}

int main()
{
  testfile("input.txt");
  testfile("docs");
  testfile("/input.txt");
  testfile("/docs");
}




Java: import java.io.File;
public class FileExistsTest {
   public static boolean isFileExists(String filename) {
       boolean exists = new File(filename).exists();
       return exists;
   }
   public static void test(String type, String filename) {
       System.out.println("The following " + type + " called " + filename + 
           (isFileExists(filename) ? " exists." : " not exists.")
       );
   }
   public static void main(String args[]) {
        test("file", "input.txt");
        test("file", File.separator + "input.txt");
        test("directory", "docs");
        test("directory", File.separator + "docs" + File.separator);
   }
}
Task: Check that file exists


C++: #include "boost/filesystem.hpp"
#include <string>
#include <iostream>

void testfile(std::string name)
{
  boost::filesystem::path file(name);
  if (exists(file))
  {
    if (is_directory(file))
      std::cout << name << " is a directory.\n";
    else
      std::cout << name << " is a non-directory file.\n";
  }
  else
    std::cout << name << " does not exist.\n";
}

int main()
{
  testfile("input.txt");
  testfile("docs");
  testfile("/input.txt");
  testfile("/docs");
}






Python: import os

os.path.isfile("input.txt")
os.path.isfile("/input.txt")
os.path.isdir("docs")
os.path.isdir("/docs")
Task: Check that file exists



C#: using System.IO;

Console.WriteLine(File.Exists("input.txt"));
Console.WriteLine(File.Exists("/input.txt"));
Console.WriteLine(Directory.Exists("docs"));
Console.WriteLine(Directory.Exists("/docs"));


Go: package main

import (
    "fmt"
    "os"
)

func printStat(p string) {
    switch i, err := os.Stat(p); {
    case err != nil:
        fmt.Println(err)
    case i.IsDir():
        fmt.Println(p, "is a directory")
    default:
        fmt.Println(p, "is a file")
    }
}

func main() {
    printStat("input.txt")
    printStat("/input.txt")
    printStat("docs")
    printStat("/docs")
}