-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandler.cpp
More file actions
53 lines (44 loc) · 1.43 KB
/
Copy pathFileHandler.cpp
File metadata and controls
53 lines (44 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// Created by Kayli Pike on 10/6/24.
//
// FileHandler.cpp
#include "FileHandler.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include "Word.h"
#include "Sentence.h"
std::string trim(const std::string& s) {
auto start = s.find_first_not_of(" \t");
auto end = s.find_last_not_of(" \t");
if (start == std::string::npos) return "";
return s.substr(start, end - start + 1);
}
void loadFlashcards(const std::string& filename, std::vector<Flashcard*>& flashcards) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error opening file: " << filename << std::endl;
return;
}
std::string line;
while (std::getline(file, line)) {
if (line.empty()) continue;
std::vector<std::string> parts;
std::stringstream ss(line);
std::string part;
while (std::getline(ss, part, '|')) {
parts.push_back(trim(part));
}
if (parts.size() != 5) {
std::cerr << "Invalid line format: " << line << std::endl;
continue;
}
std::string chineseChar = parts[0];
std::string pinyin = parts[1];
std::string meaning = parts[2];
std::string sentenceChinese = parts[3];
std::string sentenceEnglish = parts[4];
flashcards.push_back(new Word(chineseChar, pinyin, meaning));
flashcards.push_back(new Sentence(sentenceChinese, sentenceEnglish));
}
}