2023-07-21 07-53-48 4

- приведи пример на "C" : проверить если файл "test.txt" не существует, то создать его с содержимым "hello"



Конечно, вот код на C:
C
#include <stdio.h>
#include <stdlib.h>

int main() {
FILE *file;

// Check if file exists
if (file = fopen("test.txt", "r")) {
// File exists, do nothing
fclose(file);
} else {
// File does not exist, create it
file = fopen("test.txt", "w");
fprintf(file, "hello");
fclose(file);
}

return 0;
}

PHP


if (!file_exists("test.txt")) {
$file = fopen("test.txt", "w");
fwrite($file, "hello");
fclose($file);
}




#!/bin/bash

if [ ! -f test.txt ]; then
echo "hello" > test.txt
fi


#!/usr/bin/perl

if (! -e "test.txt") {
open(my $fh, "> test.txt");
print $fh "hello";
close $fh;
}


Ruby
if !File.exist?("test.txt")
File.open("test.txt", "w") do |f|
f.puts "hello"
end
end



Lisp
(if (not (probe-file "test.txt"))
(with-open-file (stream "test.txt" :direction :output)
(write-line "hello" stream))
nil)



pascal
program test;
var
file: text;

begin
if not FileExists('test.txt') then
begin
AssignFile(file, 'test.txt');
Rewrite(file);
WriteLn(file, 'hello');
CloseFile(file);
end;
end.

C#
using System;

public class Program
{
static void Main(string[] args)
{
if (!File.Exists("test.txt"))
{
System.IO.StreamWriter sw = new System.IO.StreamWriter("test.txt");
sw.WriteLine("hello");
sw.Close();
}
}
}


Forth:
: test
if file-exists? "test.txt" do
." File already exists" cr
else
create "test.txt" file open
"hello" file write
file close
end
;


Nim
import os

if not os.path.exists("test.txt"):
open("test.txt", "w").write("hello")



ASM:

section .data
file_name db "test.txt", 0

section .text
global _start

_start:
mov eax, 5 ; open file
mov ebx, file_name
mov ecx, 0 ; read/write
mov edx, 0 ; no attributes
int 0x80

mov eax, 3 ; write to file
mov ebx, eax ; file descriptor
mov ecx, "hello"
mov edx, 6 ; string length
int 0x80

mov eax, 6 ; close file
mov ebx, eax ; file descriptor
int 0x80

mov eax, 1 ; exit
mov ebx, 0 ; return code
int 0x80



Java
import java.io.File;
import java.io.IOException;

public class Main {

public static void main(String[] args) throws IOException {
File file = new File("test.txt");
if (!file.exists()) {
file.createNewFile();
FileWriter writer = new FileWriter(file);
writer.write("hello");
writer.close();
}
}
}


JavaScript
const fs = require("fs");

const file = "test.txt";

if (!fs.existsSync(file)) {
fs.writeFileSync(file, "hello");
}


COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. TEST.

DATA DIVISION.
FILE SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO "TEST.TXT".

WORKING-STORAGE SECTION.
01 FILE-1-STATUS.
05 F1-OPEN-STATUS PIC X.

PROCEDURE DIVISION.
MAIN-PROCEDURE.
OPEN INPUT FILE-1.
IF F1-OPEN-STATUS NOT = "00"
THEN
DISPLAY "File already exists".
ELSE
WRITE "hello" TO FILE-1.
CLOSE FILE-1.

STOP RUN.


APL:
∇ f(file)
if file.exists then do
print("File already exists")
end
else do
file.create
file.write("hello")
end
end

f("test.txt")



Fortran
program test

implicit none

character(len=8) :: file_name = "test.txt"

open(1, file=file_name, status="new")
if (status /= 0) then
print *, "File already exists"
else
write(1, *) "hello"
end if

close(1)

end program test


Ada:
with Ada.Text_IO;

procedure Test is
File : File_Type;
begin
Open (File, "test.txt", Mode => Out_File);
if File'Status /= 0 then
Ada.Text_IO.Put_Line ("File already exists");
else
Ada.Text_IO.Put_Line ("hello");
end if;
Close (File);
end Test;


Солнце 0.0123s .