> For the complete documentation index, see [llms.txt](https://sron.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sron.gitbook.io/docs/functions-in-sron/file-manipulation.md).

# File Functions

#### These functions are used to read and write text files in SRON. *Make note that SRON doesn't provides the functionality to work on binary files, you can only read/write text files only.*

#### SRON provides **14** functions for this:

> * freader()
> * fwriter()
> * fclose()
> * fisOpen
> * fisEnd
> * fgetName()
> * freset()
> * fcurPos()
> * fsetSeek()
> * freadChar()
> * freadLine()
> * freadWhole()
> * fwriteChar()
> * fwriteString()

***

### **1. freader():**

* This function is used to read the text file.
* It returns a 'FileReader' object.

```js
{
    name : Main

    FileReader file1 = freader("file.txt")

    Any file2 = freader("file.txt")

    println("Files Loaded!")
}
```

#### **OUTPUT :**

> Files Loaded!

***

### **2. fwriter():**

* This function is used to create a writeable text file.
* If the mentioned file doesn't exists, then it creates one.
* If the file already exists, then it clears the content of the file and writes the fresh new content.
* If you want to retain the current content of the file, then pass 'true' as the second argument which tells the function to open the file in append mode.

```js
{
    name : Main
    
    FileWriter file1 = fwriter("file.txt", false)

    @ if you want to open a file in append mode, pass true
    FileWriter file2 = fwriter("file.txt", true)

    println("Files loaded!")
}
```

#### **OUTPUT :**

> Files loaded!

***

### **3. fclose():**

* This function is used to close a file.
* Even if you forget to call it, SRON's Garbage collector does it for you so you don't have to worry.

```js
{
    name : Main

    Any file1 = freader("file.txt")

    Any file2 = fwriter("file.txt", true)

    fclose(file1)

    fclose(file2)

    println("Files loaded and closed!")
}
```

#### **OUTPUT :**

> Files loaded and closed!

***

### **4. fisOpen:**

* This function is check if the file is loaded succesfully or not.
* Returns true if file is loaded/created otherwise false.

```js
{
    name : Main
    
    Any file = freader("file.txt")

    if : {
        condition : fisOpen(file)
        println("File Load Success!")
    }
    else : {
        println("File doesn't exists!")
    }
}
```

#### **OUTPUT :**

> File doesn't exists!

***

### **5. fisEnd:**

* This function is used to check if you have reached the end while reading the file.
* If you have reached the end, it will return true otherwise false.
* It can only be used for FileReader object.

```js
{
    name : Main
    
    FileReader file = freader("file.txt")

    while : {
        condition : ${ ! fisEnd(file) }

        @ freadChar() is used to read a 
        @ single character from the file
        print(freadChar(file))        
    }
}
```

#### **OUTPUT :**

> Hello, SRON! Today is June 27, 2003.

***

### **6. fgetName():**

* This function is used to know the name of the file you are reading/writing.
* Returns a 'String' containing file name.
* If the file is closed or failed to load, then it returns an empty string.
* Can be used to know the name of both FileReader and FileWriter files.

```js
{
    name : Main
    
    FileWriter file = fwriter("file.txt", true)

    String file_name = fgetName(file)

    println(file_name)

    fclose(file)

    @ after closing, file name is an empty string
    file_name = fgetName(file)

    println("After closing, file name = '", file_name, "'")
}
```

#### **OUTPUT :**

> file.txt\
> After closing, file name = ''

***

### **7. freset():**

This function is used to reset the file by putting the seek to the start of the file.

```js
{
    name : Main
    
    FileReader file = freader("file.txt")

    @ reading first line
    String str = freadLine(file)

    println(str)

    @ reading second line
    str = freadLine(file)

    println(str)

    @ reset the file
    freset(file)
    
    @ Now the file will be read again from first line
    str = freadLine(file)

    println(str)
}
```

#### **OUTPUT :**

> First line\
> Second line\
> First line

***

### **8. fcurPos():**

* This function is used to check at what position is your seek at.
* Returns an 'Int' value syndicating the index.

```js
{
    name : Main
    
    FileReader file = freader("file.txt")

    Int cur_pos = fcurPos(file)

    println("Current Position= ", cur_pos)

    @ calling freadChar() 5 times
    for : {
        range : (Int i = 0, 5)
        freadChar(file)
    }

    cur_pos = fcurPos(file)

    println("Current Position= ", cur_pos)
}
```

#### **OUTPUT :**

> Current Position= 0\
> Current Position= 5

***

### **9. fsetSeek():**

This function is used to set the seek's position on the file.

```js
{
    name : Main

    FileReader file = freader("file.txt")

    fsetSeek(file, 2)

    println(fcurPos(file))

    fsetSeek(file, 10)

    println(fcurPos(file))
    
}
```

#### **OUTPUT :**

> 2\
> 10

***

### **10. freadChar():**

* This function is used to read a single character from file.
* It returns '\0'(Ascii 0) if the file is
  * closed,
  * failed to load
  * or reached the end.
* After reading the single character, the seek is incremented one position.

```js
{
    name : Main
    
    FileReader file = freader("file.txt")

    Char ch = freadChar(file)

    println(ch)

    ch = freadChar(file)

    println(ch)
}
```

#### **OUTPUT :**

> S\
> R

***

### **11. freadLine():**

* This function is used to read a line from the file.
* If file's seek is reached at the end, then it returns an empty string.

```js
{
    name : Main
    
    FileReader file = freader("file.txt")

    println(freadLine(file))

    println(freadLine(file))
}
```

#### **OUTPUT :**

> Hello, there\
> Welcome to Saksham Rapid Object Notation

***

### **12. freadWhole():**

This function is used to read all the content of the file after the current seek position.

```js
{
    name : Main
    
    FileReader file = freader("file.txt")

    println(freadWhole(file))
}
```

#### **OUTPUT :**

> Hello, there!!\
> Welcome to Saksham Rapid Object Notation.\
> You must be enjoying learning SRON.\
> All the best for your great journey.

***

### **13. fwriteChar():**

* This function is used to write a single character to a file.

```js
{
    name : Main

    FileWriter file = fwriter("file.txt", false)

    fwriteChar(file, 'S')

    fclose(file)
    
}
```

#### **OUTPUT :**

> // file.txt\
> S

***

### **14. fwriteString():**

* This function is used to write a string to a file.

```js
{
    name : Main
    
    FileWriter file = fwriter("file.txt", false)

    fwriteString(file, "27062003")
}
```

#### **OUTPUT :**

> // file.txt\
> 27062003

***


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://sron.gitbook.io/docs/functions-in-sron/file-manipulation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
