WeixiangYan/CodeTransOcean huggingface dataset:
Task: Read a file line by line




Go: package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
)

func init() {
	log.SetFlags(log.Lshortfile)
}

func main() {
	
	inputFile, err := os.Open("byline.go")
	if err != nil {
		log.Fatal("Error opening input file:", err)
	}

	
	
	
	defer inputFile.Close()

	scanner := bufio.NewScanner(inputFile)

	
	for scanner.Scan() {
		fmt.Println(scanner.Text())
	}

	
	
	if err := scanner.Err(); err != nil {
		log.Fatal(scanner.Err())
	}
}






COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. read-file-line-by-line.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT input-file ASSIGN TO "input.txt"
               ORGANIZATION LINE SEQUENTIAL
               FILE STATUS input-file-status.

       DATA DIVISION.
       FILE SECTION.
       FD  input-file.
       01  input-record PIC X(256).

       WORKING-STORAGE SECTION.
       01  input-file-status PIC 99.
           88  file-is-ok    VALUE 0.
           88  end-of-file   VALUE 10.

       01  line-count        PIC 9(6).

       PROCEDURE DIVISION.
           OPEN INPUT input-file
           IF NOT file-is-ok
               DISPLAY "The file could not be opened."
               GOBACK
           END-IF

           PERFORM VARYING line-count FROM 1 BY 1 UNTIL end-of-file
               READ input-file
               DISPLAY line-count ": " FUNCTION TRIM(input-record)
           END-PERFORM

           CLOSE input-file

           GOBACK
           .
Task: Read a file line by line






PHP: <?php
$file = fopen(__FILE__, 'r'); // read current file
while ($line = fgets($file)) {
    $line = rtrim($line);      // removes linebreaks and spaces at end
    echo strrev($line) . "\n"; // reverse line and upload it
}
Task: Read a file line by line






PHP: <?php
$file = fopen(__FILE__, 'r'); // read current file
while ($line = fgets($file)) {
    $line = rtrim($line);      // removes linebreaks and spaces at end
    echo strrev($line) . "\n"; // reverse line and upload it
}






Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Line_By_Line is
   File : File_Type;
begin
   Open (File => File,
         Mode => In_File,
         Name => "line_by_line.adb");
   While not  End_Of_File (File) Loop
      Put_Line (Get_Line (File));
   end loop;

   Close (File);
end Line_By_Line;
Task: Read a file line by line






PHP: <?php
$file = fopen(__FILE__, 'r'); // read current file
while ($line = fgets($file)) {
    $line = rtrim($line);      // removes linebreaks and spaces at end
    echo strrev($line) . "\n"; // reverse line and upload it
}




COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. read-file-line-by-line.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT input-file ASSIGN TO "input.txt"
               ORGANIZATION LINE SEQUENTIAL
               FILE STATUS input-file-status.

       DATA DIVISION.
       FILE SECTION.
       FD  input-file.
       01  input-record PIC X(256).

       WORKING-STORAGE SECTION.
       01  input-file-status PIC 99.
           88  file-is-ok    VALUE 0.
           88  end-of-file   VALUE 10.

       01  line-count        PIC 9(6).

       PROCEDURE DIVISION.
           OPEN INPUT input-file
           IF NOT file-is-ok
               DISPLAY "The file could not be opened."
               GOBACK
           END-IF

           PERFORM VARYING line-count FROM 1 BY 1 UNTIL end-of-file
               READ input-file
               DISPLAY line-count ": " FUNCTION TRIM(input-record)
           END-PERFORM

           CLOSE input-file

           GOBACK
           .
Task: Write entire file



C#: System.IO.File.WriteAllText("filename.txt", "This file contains a string.");









Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Write_Whole_File is
   File_Name : constant String := "the_file.txt";

   F : File_Type;
begin
   begin
      Open (F, Mode => Out_File, Name => File_Name);
   exception
      when Name_Error => Create (F, Mode => Out_File, Name => File_Name);
   end;

   Put (F, "(Over)write a file so that it contains a string. "               &
           "The reverse of Read entire file—for when you want to update or " &
           "create a file which you would read in its entirety all at once.");
   Close (F);
end Write_Whole_File;
Task: Write entire file

C: 
#define _CRT_SECURE_NO_WARNINGS  
#include <stdio.h>

int main(void)
{
    return 0 >= fputs("ANY STRING TO WRITE TO A FILE AT ONCE.", 
        freopen("sample.txt","wb",stdout));
}











Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Write_Whole_File is
   File_Name : constant String := "the_file.txt";

   F : File_Type;
begin
   begin
      Open (F, Mode => Out_File, Name => File_Name);
   exception
      when Name_Error => Create (F, Mode => Out_File, Name => File_Name);
   end;

   Put (F, "(Over)write a file so that it contains a string. "               &
           "The reverse of Read entire file—for when you want to update or " &
           "create a file which you would read in its entirety all at once.");
   Close (F);
end Write_Whole_File;
Task: Write entire file


C++: #include <fstream>
using namespace std;

int main()
{
    ofstream file("new.txt");
    file << "this is a string";
    file.close();
    return 0;
}










Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Write_Whole_File is
   File_Name : constant String := "the_file.txt";

   F : File_Type;
begin
   begin
      Open (F, Mode => Out_File, Name => File_Name);
   exception
      when Name_Error => Create (F, Mode => Out_File, Name => File_Name);
   end;

   Put (F, "(Over)write a file so that it contains a string. "               &
           "The reverse of Read entire file—for when you want to update or " &
           "create a file which you would read in its entirety all at once.");
   Close (F);
end Write_Whole_File;
Task: Write entire file




Go: import "io/ioutil"

func main() {
    ioutil.WriteFile("path/to/your.file", []byte("data"), 0644)
}








Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Write_Whole_File is
   File_Name : constant String := "the_file.txt";

   F : File_Type;
begin
   begin
      Open (F, Mode => Out_File, Name => File_Name);
   exception
      when Name_Error => Create (F, Mode => Out_File, Name => File_Name);
   end;

   Put (F, "(Over)write a file so that it contains a string. "               &
           "The reverse of Read entire file—for when you want to update or " &
           "create a file which you would read in its entirety all at once.");
   Close (F);
end Write_Whole_File;
Task: Write entire file





Java: import java.io.*;

public class Test {

    public static void main(String[] args) throws IOException {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt"))) {
            bw.write("abc");
        }
    }
}







Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Write_Whole_File is
   File_Name : constant String := "the_file.txt";

   F : File_Type;
begin
   begin
      Open (F, Mode => Out_File, Name => File_Name);
   exception
      when Name_Error => Create (F, Mode => Out_File, Name => File_Name);
   end;

   Put (F, "(Over)write a file so that it contains a string. "               &
           "The reverse of Read entire file—for when you want to update or " &
           "create a file which you would read in its entirety all at once.");
   Close (F);
end Write_Whole_File;
Task: Write entire file







Python: with open(filename, 'w') as f:
    f.write(data)





Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Write_Whole_File is
   File_Name : constant String := "the_file.txt";

   F : File_Type;
begin
   begin
      Open (F, Mode => Out_File, Name => File_Name);
   exception
      when Name_Error => Create (F, Mode => Out_File, Name => File_Name);
   end;

   Put (F, "(Over)write a file so that it contains a string. "               &
           "The reverse of Read entire file—for when you want to update or " &
           "create a file which you would read in its entirety all at once.");
   Close (F);
end Write_Whole_File;
Task: Write entire file








VB: Option Explicit

Const strName As String = "MyFileText.txt"
Const Text As String = "(Over)write a file so that it contains a string. " & vbCrLf & _
           "The reverse of Read entire file—for when you want to update or " & vbCrLf & _
           "create a file which you would read in its entirety all at once."

Sub Main()
Dim Nb As Integer

Nb = FreeFile
Open "C:\Users\" & Environ("username") & "\Desktop\" & strName For Output As #Nb
    Print #1, Text
Close #Nb
End Sub




Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Write_Whole_File is
   File_Name : constant String := "the_file.txt";

   F : File_Type;
begin
   begin
      Open (F, Mode => Out_File, Name => File_Name);
   exception
      when Name_Error => Create (F, Mode => Out_File, Name => File_Name);
   end;

   Put (F, "(Over)write a file so that it contains a string. "               &
           "The reverse of Read entire file—for when you want to update or " &
           "create a file which you would read in its entirety all at once.");
   Close (F);
end Write_Whole_File;
Task: Write entire file

C: 
#define _CRT_SECURE_NO_WARNINGS  
#include <stdio.h>

int main(void)
{
    return 0 >= fputs("ANY STRING TO WRITE TO A FILE AT ONCE.", 
        freopen("sample.txt","wb",stdout));
}
Task: Write entire file


C++: #include <fstream>
using namespace std;

int main()
{
    ofstream file("new.txt");
    file << "this is a string";
    file.close();
    return 0;
}
Task: Write entire file





Java: import java.io.*;

public class Test {

    public static void main(String[] args) throws IOException {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt"))) {
            bw.write("abc");
        }
    }
}
Task: Write entire file








VB: Option Explicit

Const strName As String = "MyFileText.txt"
Const Text As String = "(Over)write a file so that it contains a string. " & vbCrLf & _
           "The reverse of Read entire file—for when you want to update or " & vbCrLf & _
           "create a file which you would read in its entirety all at once."

Sub Main()
Dim Nb As Integer

Nb = FreeFile
Open "C:\Users\" & Environ("username") & "\Desktop\" & strName For Output As #Nb
    Print #1, Text
Close #Nb
End Sub
Task: Write entire file




Go: import "io/ioutil"

func main() {
    ioutil.WriteFile("path/to/your.file", []byte("data"), 0644)
}
Task: Write entire file



C#: System.IO.File.WriteAllText("filename.txt", "This file contains a string.");
Task: Write entire file







Python: with open(filename, 'w') as f:
    f.write(data)
Task: Write entire file



C#: System.IO.File.WriteAllText("filename.txt", "This file contains a string.");








Fortran:       OPEN (F,FILE="SomeFileName.txt",STATUS="REPLACE")
      WRITE (F,*) "Whatever you like."
      WRITE (F) BIGARRAY
Task: Write entire file


C++: #include <fstream>
using namespace std;

int main()
{
    ofstream file("new.txt");
    file << "this is a string";
    file.close();
    return 0;
}









Fortran:       OPEN (F,FILE="SomeFileName.txt",STATUS="REPLACE")
      WRITE (F,*) "Whatever you like."
      WRITE (F) BIGARRAY
Task: Write entire file

C: 
#define _CRT_SECURE_NO_WARNINGS  
#include <stdio.h>

int main(void)
{
    return 0 >= fputs("ANY STRING TO WRITE TO A FILE AT ONCE.", 
        freopen("sample.txt","wb",stdout));
}










Fortran:       OPEN (F,FILE="SomeFileName.txt",STATUS="REPLACE")
      WRITE (F,*) "Whatever you like."
      WRITE (F) BIGARRAY
Task: Write entire file





Java: import java.io.*;

public class Test {

    public static void main(String[] args) throws IOException {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt"))) {
            bw.write("abc");
        }
    }
}






Fortran:       OPEN (F,FILE="SomeFileName.txt",STATUS="REPLACE")
      WRITE (F,*) "Whatever you like."
      WRITE (F) BIGARRAY
Task: Write entire file







Python: with open(filename, 'w') as f:
    f.write(data)




Fortran:       OPEN (F,FILE="SomeFileName.txt",STATUS="REPLACE")
      WRITE (F,*) "Whatever you like."
      WRITE (F) BIGARRAY
Task: Write entire file








VB: Option Explicit

Const strName As String = "MyFileText.txt"
Const Text As String = "(Over)write a file so that it contains a string. " & vbCrLf & _
           "The reverse of Read entire file—for when you want to update or " & vbCrLf & _
           "create a file which you would read in its entirety all at once."

Sub Main()
Dim Nb As Integer

Nb = FreeFile
Open "C:\Users\" & Environ("username") & "\Desktop\" & strName For Output As #Nb
    Print #1, Text
Close #Nb
End Sub



Fortran:       OPEN (F,FILE="SomeFileName.txt",STATUS="REPLACE")
      WRITE (F,*) "Whatever you like."
      WRITE (F) BIGARRAY
Task: Write entire file






PHP: file_put_contents($filename, $data)





Fortran:       OPEN (F,FILE="SomeFileName.txt",STATUS="REPLACE")
      WRITE (F,*) "Whatever you like."
      WRITE (F) BIGARRAY
Task: Write entire file

C: 
#define _CRT_SECURE_NO_WARNINGS  
#include <stdio.h>

int main(void)
{
    return 0 >= fputs("ANY STRING TO WRITE TO A FILE AT ONCE.", 
        freopen("sample.txt","wb",stdout));
}









COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Overwrite.
       AUTHOR. Bill Gunshannon. 
       INSTALLATION.  Home.
       DATE-WRITTEN.  31 December 2021.
      ************************************************************
      ** Program Abstract:
      **   Simple COBOL task.  Open file for output.  Write
      **     data to file. Close file.  Done...        
      ************************************************************
       
       ENVIRONMENT DIVISION.
       
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
            SELECT File-Name ASSIGN TO "File.txt"
                 ORGANIZATION IS LINE SEQUENTIAL.
       DATA DIVISION.
       
       FILE SECTION.
       
       FD  File-Name
           DATA RECORD IS Record-Name.
       01  Record-Name.
           02 Field1                  PIC X(80).
       
       WORKING-STORAGE SECTION.
       
       01 New-Val                   PIC X(80)
              VALUE 'Hello World'.
       
       
       PROCEDURE DIVISION.
       
       Main-Program.
           OPEN OUTPUT File-Name.
           WRITE Record-Name FROM New-Val.
           CLOSE File-Name.
           STOP RUN.
       
       END-PROGRAM.
Task: Write entire file



C#: System.IO.File.WriteAllText("filename.txt", "This file contains a string.");







COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Overwrite.
       AUTHOR. Bill Gunshannon. 
       INSTALLATION.  Home.
       DATE-WRITTEN.  31 December 2021.
      ************************************************************
      ** Program Abstract:
      **   Simple COBOL task.  Open file for output.  Write
      **     data to file. Close file.  Done...        
      ************************************************************
       
       ENVIRONMENT DIVISION.
       
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
            SELECT File-Name ASSIGN TO "File.txt"
                 ORGANIZATION IS LINE SEQUENTIAL.
       DATA DIVISION.
       
       FILE SECTION.
       
       FD  File-Name
           DATA RECORD IS Record-Name.
       01  Record-Name.
           02 Field1                  PIC X(80).
       
       WORKING-STORAGE SECTION.
       
       01 New-Val                   PIC X(80)
              VALUE 'Hello World'.
       
       
       PROCEDURE DIVISION.
       
       Main-Program.
           OPEN OUTPUT File-Name.
           WRITE Record-Name FROM New-Val.
           CLOSE File-Name.
           STOP RUN.
       
       END-PROGRAM.
Task: Write entire file


C++: #include <fstream>
using namespace std;

int main()
{
    ofstream file("new.txt");
    file << "this is a string";
    file.close();
    return 0;
}








COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Overwrite.
       AUTHOR. Bill Gunshannon. 
       INSTALLATION.  Home.
       DATE-WRITTEN.  31 December 2021.
      ************************************************************
      ** Program Abstract:
      **   Simple COBOL task.  Open file for output.  Write
      **     data to file. Close file.  Done...        
      ************************************************************
       
       ENVIRONMENT DIVISION.
       
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
            SELECT File-Name ASSIGN TO "File.txt"
                 ORGANIZATION IS LINE SEQUENTIAL.
       DATA DIVISION.
       
       FILE SECTION.
       
       FD  File-Name
           DATA RECORD IS Record-Name.
       01  Record-Name.
           02 Field1                  PIC X(80).
       
       WORKING-STORAGE SECTION.
       
       01 New-Val                   PIC X(80)
              VALUE 'Hello World'.
       
       
       PROCEDURE DIVISION.
       
       Main-Program.
           OPEN OUTPUT File-Name.
           WRITE Record-Name FROM New-Val.
           CLOSE File-Name.
           STOP RUN.
       
       END-PROGRAM.
Task: Write entire file





Java: import java.io.*;

public class Test {

    public static void main(String[] args) throws IOException {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt"))) {
            bw.write("abc");
        }
    }
}





COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Overwrite.
       AUTHOR. Bill Gunshannon. 
       INSTALLATION.  Home.
       DATE-WRITTEN.  31 December 2021.
      ************************************************************
      ** Program Abstract:
      **   Simple COBOL task.  Open file for output.  Write
      **     data to file. Close file.  Done...        
      ************************************************************
       
       ENVIRONMENT DIVISION.
       
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
            SELECT File-Name ASSIGN TO "File.txt"
                 ORGANIZATION IS LINE SEQUENTIAL.
       DATA DIVISION.
       
       FILE SECTION.
       
       FD  File-Name
           DATA RECORD IS Record-Name.
       01  Record-Name.
           02 Field1                  PIC X(80).
       
       WORKING-STORAGE SECTION.
       
       01 New-Val                   PIC X(80)
              VALUE 'Hello World'.
       
       
       PROCEDURE DIVISION.
       
       Main-Program.
           OPEN OUTPUT File-Name.
           WRITE Record-Name FROM New-Val.
           CLOSE File-Name.
           STOP RUN.
       
       END-PROGRAM.
Task: Write entire file







Python: with open(filename, 'w') as f:
    f.write(data)



COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Overwrite.
       AUTHOR. Bill Gunshannon. 
       INSTALLATION.  Home.
       DATE-WRITTEN.  31 December 2021.
      ************************************************************
      ** Program Abstract:
      **   Simple COBOL task.  Open file for output.  Write
      **     data to file. Close file.  Done...        
      ************************************************************
       
       ENVIRONMENT DIVISION.
       
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
            SELECT File-Name ASSIGN TO "File.txt"
                 ORGANIZATION IS LINE SEQUENTIAL.
       DATA DIVISION.
       
       FILE SECTION.
       
       FD  File-Name
           DATA RECORD IS Record-Name.
       01  Record-Name.
           02 Field1                  PIC X(80).
       
       WORKING-STORAGE SECTION.
       
       01 New-Val                   PIC X(80)
              VALUE 'Hello World'.
       
       
       PROCEDURE DIVISION.
       
       Main-Program.
           OPEN OUTPUT File-Name.
           WRITE Record-Name FROM New-Val.
           CLOSE File-Name.
           STOP RUN.
       
       END-PROGRAM.
Task: Write entire file








VB: Option Explicit

Const strName As String = "MyFileText.txt"
Const Text As String = "(Over)write a file so that it contains a string. " & vbCrLf & _
           "The reverse of Read entire file—for when you want to update or " & vbCrLf & _
           "create a file which you would read in its entirety all at once."

Sub Main()
Dim Nb As Integer

Nb = FreeFile
Open "C:\Users\" & Environ("username") & "\Desktop\" & strName For Output As #Nb
    Print #1, Text
Close #Nb
End Sub


COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Overwrite.
       AUTHOR. Bill Gunshannon. 
       INSTALLATION.  Home.
       DATE-WRITTEN.  31 December 2021.
      ************************************************************
      ** Program Abstract:
      **   Simple COBOL task.  Open file for output.  Write
      **     data to file. Close file.  Done...        
      ************************************************************
       
       ENVIRONMENT DIVISION.
       
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
            SELECT File-Name ASSIGN TO "File.txt"
                 ORGANIZATION IS LINE SEQUENTIAL.
       DATA DIVISION.
       
       FILE SECTION.
       
       FD  File-Name
           DATA RECORD IS Record-Name.
       01  Record-Name.
           02 Field1                  PIC X(80).
       
       WORKING-STORAGE SECTION.
       
       01 New-Val                   PIC X(80)
              VALUE 'Hello World'.
       
       
       PROCEDURE DIVISION.
       
       Main-Program.
           OPEN OUTPUT File-Name.
           WRITE Record-Name FROM New-Val.
           CLOSE File-Name.
           STOP RUN.
       
       END-PROGRAM.
Task: Write entire file




Go: import "io/ioutil"

func main() {
    ioutil.WriteFile("path/to/your.file", []byte("data"), 0644)
}






COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Overwrite.
       AUTHOR. Bill Gunshannon. 
       INSTALLATION.  Home.
       DATE-WRITTEN.  31 December 2021.
      ************************************************************
      ** Program Abstract:
      **   Simple COBOL task.  Open file for output.  Write
      **     data to file. Close file.  Done...        
      ************************************************************
       
       ENVIRONMENT DIVISION.
       
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
            SELECT File-Name ASSIGN TO "File.txt"
                 ORGANIZATION IS LINE SEQUENTIAL.
       DATA DIVISION.
       
       FILE SECTION.
       
       FD  File-Name
           DATA RECORD IS Record-Name.
       01  Record-Name.
           02 Field1                  PIC X(80).
       
       WORKING-STORAGE SECTION.
       
       01 New-Val                   PIC X(80)
              VALUE 'Hello World'.
       
       
       PROCEDURE DIVISION.
       
       Main-Program.
           OPEN OUTPUT File-Name.
           WRITE Record-Name FROM New-Val.
           CLOSE File-Name.
           STOP RUN.
       
       END-PROGRAM.
Task: Write entire file






PHP: file_put_contents($filename, $data)
Task: Write entire file






PHP: file_put_contents($filename, $data)






Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Write_Whole_File is
   File_Name : constant String := "the_file.txt";

   F : File_Type;
begin
   begin
      Open (F, Mode => Out_File, Name => File_Name);
   exception
      when Name_Error => Create (F, Mode => Out_File, Name => File_Name);
   end;

   Put (F, "(Over)write a file so that it contains a string. "               &
           "The reverse of Read entire file—for when you want to update or " &
           "create a file which you would read in its entirety all at once.");
   Close (F);
end Write_Whole_File;