# 2. Открытие файлов

{% file src="/files/yLy9d7eTReIrmJMqwI8N" %}
Весь приведённый далее код оформлен с использованием clang-format
{% endfile %}

{% code overflow="wrap" lineNumbers="true" %}

```c
int main(int argc, char *argv[])
{
	FILE* inputFile = fopen(argv[1], "r");
	FILE* outputFile = fopen(argv[2], "w");

    // malloc
    // read from input file
    // calculate
    // write to output file
    
    fclose(inputFile);
    fclose(outputFile);
}
```

{% endcode %}

<details>

<summary>Вариант реорганизации и исправления кода</summary>

Освобождайте ресурсы сразу, как они становятся быть ненужными. Более того, если в `calculate` может произойти выходи из программы, то нужно будет освобождать больше ресурсов и не забыть это сделать.

{% code overflow="wrap" lineNumbers="true" %}

```c
int main(int argc, char *argv[])
{
	FILE* inputFile = fopen(argv[1], "r");
    // file is opened?
    // malloc
    // read from input file
    fclose(inputFile);

    // calculate
    
	FILE* outputFile = fopen(argv[2], "w");
    // file is opened?    
    // write to output file
    fclose(outputFile);
}
```

{% endcode %}

</details>


---

# Agent Instructions: 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:

```
GET https://skkv-itmo.gitbook.io/c-cpp-cookies/best-practices/best-practices/open-files.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
