---
title: "fread | open.mp"
description: "Read a single line from a file."
canonical: https://omp.itsneufox.com/th/docs/scripting/functions/fread
language: th
---

# fread

## คำอธิบาย

Read a single line from a file.

| Name | Description |
| --- | --- |
| handle | The handle of the file to read from (returned by fopen). |
| string | A string array to store the read text in, passed by reference. |
| size | The number of bytes to read. |
| pack | Should the string be packed? True/false. |

## ส่งคืน

The length of string (the read text) as an integer.

## ตัวอย่าง

```c
// Open "file.txt" in "read only" mode
new File:handle = fopen("file.txt", io_read),

    // Initialize "buf"
    buf[128];

// Check, if the file is opened
if (handle)
{
    // Success

    // Read the whole file
    while (fread(handle, buf)) print(buf);

    // Close the file
    fclose(handle);
}
else
{
    // Error
    print("The file \"file.txt\" does not exists, or can't be opened.");
}
```

```c
// Open "file.txt" in "read and write" mode
new File:handle = fopen("file.txt"),

    // Initialize "buf"
    buf[128];

// Check, if file is open
if (handle)
{
    // Success

    // Read the whole file
    while (fread(handle, buf)) print(buf);

    // Set the file pointer to the first byte
    fseek(handle, _, seek_begin);

    // Write "I just wrote here!" into this file
    fwrite(handle, "I just wrote here!");

    // Close the file
    fclose(handle);
}
else
{
    // Error
    print("The file \"file.txt\" does not exists, or can't be opened.");
}
```

## บันทึก

warning

Using an invalid handle will crash your server! Get a valid handle by using fopen or ftemp.

## ฟังก์ชั่นที่เกี่ยวข้องกัน

-   [fopen](https://omp.itsneufox.com/th/docs/scripting/functions/fopen): Open a file.
-   [fclose](https://omp.itsneufox.com/th/docs/scripting/functions/fclose): Close a file.
-   [ftemp](https://omp.itsneufox.com/th/docs/scripting/functions/ftemp): Create a temporary file stream.
-   [fremove](https://omp.itsneufox.com/th/docs/scripting/functions/fremove): Remove a file.
-   [fwrite](https://omp.itsneufox.com/th/docs/scripting/functions/fwrite): Write to a file.
-   [fread](https://omp.itsneufox.com/th/docs/scripting/functions/fread): Read a file.
-   [fputchar](https://omp.itsneufox.com/th/docs/scripting/functions/fputchar): Put a character in a file.
-   [fgetchar](https://omp.itsneufox.com/th/docs/scripting/functions/fgetchar): Get a character from a file.
-   [fblockwrite](https://omp.itsneufox.com/th/docs/scripting/functions/fblockwrite): Write blocks of data into a file.
-   [fblockread](https://omp.itsneufox.com/th/docs/scripting/functions/fblockread): Read blocks of data from a file.
-   [fseek](https://omp.itsneufox.com/th/docs/scripting/functions/fseek): Jump to a specific character in a file.
-   [flength](https://omp.itsneufox.com/th/docs/scripting/functions/flength): Get the file length.
-   [fexist](https://omp.itsneufox.com/th/docs/scripting/functions/fexist): Check, if a file exists.
-   [fmatch](https://omp.itsneufox.com/th/docs/scripting/functions/fmatch): Check, if patterns with a file name matches.
