# Lezen en schrijven naar bestanden

Bij testen is het heel handig als je niet steeds opnieuw de test-cases uit hoeft te typen. Dat hoeft ook niet, je kan namelijk lezen uit bestanden. Hierbij heb je de informatie snel bij de hand. Dit wordt soms ook bij wedstrijden gebruikt.

In dit hoofdstuk leer je hoe je invoerfiles kunt lezen in C++.

## Schrijven naar een tekstbestand

Eerder heb je geleerd om uitvoer op het scherm te zetten met `cout`. Schrijven naar een tekstbestand werkt bijna hetzelfde, alleen moet je eerst een bestand openen.

Daarvoor gebruik je `ofstream` (output file stream). Bijvoorbeeld:

* `ofstream myfile;` maakt een variabele waarmee je naar een bestand kunt schrijven.
* Daarna open je het bestand met `myfile.open(...)`.

Je kunt een **relatief pad** gebruiken (bijvoorbeeld `"example.txt"`). Dan wordt het bestand gemaakt in de huidige map. Je kunt ook een **volledig pad** gebruiken, zoals:

`"C:\\Users\\...\\example.txt"`

> Let op: in C++ moet je in Windows-paden dubbele backslashes gebruiken (`\\`).

Schrijven doe je met `<<`, net als bij `cout`. Vergeet niet om het bestand aan het einde te sluiten met `close()`.

Voorbeeld:

```
// writing to a text file
#include <iostream>
#include <fstream>
using namespace std;

int main() {
  ofstream myfile;
  myfile.open("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}
```

## Lezen uit een tekstbestand

Lezen uit een bestand werkt vergelijkbaar met `cin`. Als je weet dat het bestand bijvoorbeeld drie getallen bevat, kun je die inlezen met `>>`.

Als je bijvoorbeeld weet dat bestand "getallen.txt" drie getallen heeft, dan kun je onderstaande code gebruiken om die drie getallen in te lezen.

```
// reading a text file
#include <iostream>
#include <fstream>
using namespace std;

int main() {
  int a, b, c;
  ifstream myfile("getallen.txt");
  myfile >> a >> b >> c;
  myfile.close();
  return 0;
}
```

#### Lezen met een onbekend aantal regels

Als je niet weet hoeveel regels het bestand heeft, kun je per regel inlezen met `getline`. Zolang er nog regels zijn, blijft het programma lezen.

Dit is een voorbeeldprogramma waarbij alle regels uit een bestand gelezen worden en geschreven worden naar standaard-output.

```
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) && line != "")
    {
      cout << line << '\n';
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}
```

Let wel, met bovenstaande code lees je de regels in als string.


---

# 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://girls.gitbook.io/c++-cursus/lezen-en-schrijven-naar-bestanden.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.
