Прочитайте из txt-файла и запишите в картуC++

Программы на C++. Форум разработчиков
Anonymous
Прочитайте из txt-файла и запишите в карту

Сообщение Anonymous »

C++
Я пытаюсь прочитать данные из файла .txt и сохранить их в переменной карты.
Но когда я пытаюсь распечатав карту, я получаю ровно 8 пустых строк.
Я использовал gpt для создания определенной части кода (я отметил это в строке комментария)
Я есть текстовый файл с именем "PUBLISHERS.txt":
1 Apress Media
2 John-Wiley
3 McGraw-Hill
4 Microsoft Press
5 Packt Publishing
6 Pearson Education
7 Prentice-Hall

и main.cpp:
#include
#include
#include
#include
#include

using namespace std;

int main() {
// Open Files
ifstream publishers_file("../Publishers.txt");

// Error Log if failed
if (!publishers_file.is_open()) {
cout publisherid) {

getline(publishers_file, publishername);

//below this line is generated by gpt
size_t startpos = publishername.find_first_not_of(" \t");
if (startpos != string::npos) {
publishername = publishername.substr(startpos);
} else {
publishername.clear();
}

if (!publishername.empty()) {
publishers[publisherid] = publishername;
}
}

publishers_file.close();

// Test
int i = 0;
for (const auto& pair : publishers) {
cout

Подробнее здесь: https://stackoverflow.com/questions/785 ... int-string

Вернуться в «C++»