fopen

fopen(string $filename, string $mode, bool $use_include_path = false, ?resource $context = null): resource|false

Opens a file and returns a stream handle (resource) that the whole f* family operates on; complements the fopen_modes concept entry - this entry focuses on the handle you get back.

C C23

C23
#include <stdio.h>

int main(void) {
    // fopen binds a path to a FILE* handle; every f* call operates on it
    FILE *out = fopen("demo.txt", "w");
    size_t written = fwrite("data", 1, 4, out);
    fclose(out); // the handle is released, the file stays on disk

    printf("written: %zu\n", written);
    printf("handle: FILE*\n");

    FILE *in = fopen("demo.txt", "r");
    char buf[8] = {0};
    size_t n = fread(buf, 1, 4, in);
    buf[n] = '\0';
    fclose(in);
    printf("read back: %s\n", buf);
    return 0;
}
Output
written: 4
handle: FILE*
read back: data

C++ C++26

C++26
#include <fstream>
#include <iostream>
#include <string>

int main() {
    // std::ofstream / std::ifstream are RAII handles: the destructor closes the file
    {
        std::ofstream out("demo.txt");
        out << "data";
    } // closed automatically here
    std::cout << "written: 4\n";
    std::cout << "handle: ofstream/ifstream\n";
    std::ifstream in("demo.txt");
    std::string s((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
    std::cout << "read back: " << s << "\n";
}
Output
written: 4
handle: ofstream/ifstream
read back: data

C# v11.0.100

v11.0.100
// FileStream is the handle itself; StreamWriter/StreamReader layer text encoding on top
using System.IO;

File.WriteAllText("demo.txt", "data"); // opens a FileStream, writes, closes - same handle underneath
Console.WriteLine("written: 4");
Console.WriteLine("handle: FileStream");
Console.WriteLine("read back: " + File.ReadAllText("demo.txt"));
Output
written: 4
handle: FileStream
read back: data

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"os"
)

func main() {
	// os.File is the handle; Read/Write/Seek/Close all hang off it
	f, err := os.Create("demo.txt")
	if err != nil {
		panic(err)
	}
	n, err := f.WriteString("data")
	if err != nil {
		panic(err)
	}
	f.Close()
	fmt.Printf("written: %d\n", n)
	fmt.Println("handle: *os.File")
	b, err := os.ReadFile("demo.txt")
	if err != nil {
		panic(err)
	}
	fmt.Println("read back: " + string(b))
}
Output
written: 4
handle: *os.File
read back: data

Java v27.0.1

v27.0.1
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

public class Main {
    public static void main(String[] args) throws Exception {
        // FileOutputStream / FileInputStream are the raw handle pair
        FileOutputStream out = new FileOutputStream("demo.txt");
        out.write("data".getBytes(StandardCharsets.UTF_8));
        out.close();
        System.out.println("written: 4");
        System.out.println("handle: FileOutputStream");
        FileInputStream in = new FileInputStream("demo.txt");
        byte[] buf = new byte[8];
        int n = in.read(buf);
        in.close();
        System.out.println("read back: " + new String(buf, 0, n, StandardCharsets.UTF_8));
    }
}
Output
written: 4
handle: FileOutputStream
read back: data

JavaScript v26.13.1

v26.13.1
// Node opens files as fs.FileHandle (promises API); the sync facade returns a numeric fd
const fs = require("node:fs");
const out = fs.openSync("demo.txt", "w");
const written = fs.writeSync(out, "data");
fs.closeSync(out);
console.log("written:", written);
console.log("handle: fs.FileHandle");
console.log("read back:", fs.readFileSync("demo.txt", "utf8"));
Output
written: 4
handle: fs.FileHandle
read back: data

PHP v8.6.0

v8.6.0
// fopen returns a stream resource - the handle every f* function operates on
$h = fopen("demo.txt", "w");
$written = fwrite($h, "data");
fclose($h);
echo "written: $written\n";
$in = fopen("demo.txt", "r");
echo "handle: ", get_resource_type($in), "\n";
echo "read back: ", fread($in, 4), "\n";
fclose($in);
Output
written: 4
handle: stream
read back: data

Python v3.15.2

v3.15.2
# open() returns a file object (TextIOWrapper in text mode) - the handle
with open("demo.txt", "w") as f:
    written = f.write("data")
print(f"written: {written}")
print("handle: TextIOWrapper")
with open("demo.txt", "r") as f:
    print("read back:", f.read(4))
Output
written: 4
handle: TextIOWrapper
read back: data

Rust v1.99.1

v1.99.1
use std::fs::File;
use std::io::{Read, Write};

fn main() {
    // fs::File is the handle; Read/Write are traits you must bring into scope
    let mut out = File::create("demo.txt").unwrap();
    let written = out.write(b"data").unwrap();
    println!("written: {}", written);
    println!("handle: fs::File");
    let mut inp = File::open("demo.txt").unwrap();
    let mut s = String::new();
    inp.read_to_string(&mut s).unwrap();
    println!("read back: {}", s);
}
Output
written: 4
handle: fs::File
read back: data

TypeScript v26.13.1

v26.13.1
// Node opens files as fs.FileHandle (promises API); the sync facade returns a numeric fd
import * as fs from "node:fs";
const out = fs.openSync("demo.txt", "w");
const written: number = fs.writeSync(out, "data");
fs.closeSync(out);
console.log("written:", written);
console.log("handle: fs.FileHandle");
console.log("read back:", fs.readFileSync("demo.txt", "utf8"));
Output
written: 4
handle: fs.FileHandle
read back: data

Language differences

C: fopen returns a FILE* handle that every f* function takes as first argument; fclose only releases the handle, the file stays.

C++: std::ofstream/std::ifstream are RAII handles - destruction closes the file; the read-write std::fstream appears in the fread_fwrite entry.

C#: FileStream is the handle itself; StreamWriter/StreamReader layer text encoding on top; one-shot APIs like File.ReadAllText open/handle/close the same handle internally.

Go: os.Create/os.Open return the *os.File handle; Read/Write/Seek/Close are its methods.

Java: FileOutputStream/FileInputStream are the raw byte handle pair; use FileReader/FileWriter at the char level - this sample uses bytes with explicit UTF-8.

JavaScript: Node's sync API returns a numeric fd; the promises API fs.promises.open returns an fs.FileHandle object; browser JS has no file handle concept at all.

PHP: fopen returns a stream resource (get_resource_type shows "stream") that every f* function takes as first argument; mode semantics live in the fopen_modes entry.

Python: open() returns a file object (io.TextIOWrapper in text mode); the with statement closes it on exit.

Rust: fs::File is the handle; Read/Write are traits, not built-in methods - you must use std::io::{Read, Write}.

TypeScript: same as JavaScript; type annotations go on the handle-operation return values.