0

проблема такая, мне нужно показать, что строка заканчивается и на символе "," и на символе ";".

void read() {
        ifstream file;
        file.open("students.txt");
        if (file.is_open()) {
            cout << "Файл не считан!" << endl;
        }
        else {
            string str;
            while (!file.eof()) {
                str = "";
                getline(file, str, ',');
                cout << str << endl;
            }
        }
    }

1 Answers1

-1

Ну например можно написать вот так:

void read() {
    ifstream file;
    file.open("students.txt");
    if (!file.is_open())
        cout << "Файл не считан!" << endl;
    else {
        string str;
        //Считываем весь файл
        getline(file, str, '\0');
        //strtok разделяет строку по разделителям
        char* token = strtok((char*)str.c_str(), ",;");
        while (token != nullptr) {
            cout << token << endl;
            token = strtok(NULL, ",;");
        }
    }
}

Хочу заметить, что у вас есть строчка if(file.is_open()) то есть если файл открыт, то выводим Файл не считан!.