CodeBlanks Logo Icon CodeBlanks

Dosya I/O

Verileri dosyaya kaydedip yüklersin.

Dosya I/O: kaydet ve yükle

Bu derste görevleri dosyaya kaydedip geri yükleyeceğiz. std::ofstream ile yazar, std::ifstream ile okuruz.

Örnek: std::ofstream out("tasks.txt"); out << t.title << " " << t.done;. Bu, bir görevin başlığını ve durumunu dosyaya yazar. Geri okumak için std::ifstream in("tasks.txt"); in >> title >> done;. Birden çok görevi bir döngüde yazıp okuyabilirsin. Önceki derslerin Task sınıfı ve printItem şablonunu alıp üzerine dosya I/O ekleyeceğiz.

Döngüde kaydet ve yükle

Birden çok görevi kaydetmek için bir for döngüsü kullanırsın. Geri yüklerken while (in >> title >> done) döngüsü dosya sonuna kadar okur.

Örnek: for (const auto& t : tasks) { out << t.title << " " << t.done << "\n"; }. Bu, her görevi bir satıra yazar. Geri okurken while (in >> title >> done) { count++; } her başarılı okumada sayacı artırır. >> boşlukla ayrılmış veriyi çeker; \n sonraki okumaya geçmeyi sağlar. Bu desen, kaydet-yükle işlemlerinin temelidir.

Uygulama Görevleri dosyaya kaydet ve geri oku

Başlangıç kodundaki Task sınıfı ve printItem şablonunun ardına main içinde bir std::vector<Task> oluştur. İki görev ekle (Ödev, Spor). std::ofstream ile görevleri tasks.txt'ye kaydet. std::ifstream ile geri oku, say ve std::cout ile yazdır.

Başlangıç kodu

main.cpp
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

class Task {
public:
    std::string title;
    bool done;
    Task(std::string t) : title(t), done(false) {}
};

template <typename T>
void printItem(const T& item) {
    std::cout << item;
}

int main() {
    // Vector'e iki görev ekle, dosyaya kaydet, geri oku ve say
    return 0;
}

Örnek çözüm

main.cpp
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

class Task {
public:
    std::string title;
    bool done;
    Task(std::string t) : title(t), done(false) {}
};

template <typename T>
void printItem(const T& item) {
    std::cout << item;
}

int main() {
    std::vector<Task> tasks;
    tasks.push_back(Task("Ödev"));
    tasks.push_back(Task("Spor"));
    std::ofstream out("tasks.txt");
    for (const auto& t : tasks) {
        out << t.title << " " << t.done << "\n";
    }
    out.close();
    std::ifstream in("tasks.txt");
    std::string title;
    bool done;
    int c = 0;
    while (in >> title >> done) {
        c++;
    }
    std::cout << c;
    return 0;
}

Beklenen çıktı

2

Soru ve cevaplar

Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.

1 Görevleri dosyaya kaydetmek için hangi sınıf?

main.cpp
std::ofstream out("tasks.txt");
  • std::ifstream
  • std::ofstream Doğru cevap
  • std::vector
  • std::cout

Açıklama: std::ofstream dosyaya yazmak için kullanılır.

2 Dosyadan oku

Soru
std::____ in("tasks.txt");
Cevap
std::ifstream in("tasks.txt");

Açıklama: std::ifstream dosyadan okur.

3 Parçayı rolüne eşle

Kavram Anlamı
std::ofstream Dosyaya yazma
std::ifstream Dosyadan okuma
out << t.title Başlık yazma
while (in >> ...) Döngüde okuma

Orta seviye bölümündeki tüm dersler