1

как мне сделать так, чтобы во 2 файл переписалось все, кроме латинских букв, как реализовать саму проверку на латинские буквы. Заранее спасибо

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    setlocale(LC_ALL, "RU");
    ifstream t1("file1.txt");
    ofstream t2("file2.txt");
    if (!t1) 
    { 
        cout << "Error of opening of a input-file ";       
        return 0; 
    }
    if (t1 && t2 ) 
    {
        char t;
        if (!t2)
        {
            cout << "Error of opening of a input-file ";
            return 0;
        }

        while (!t1.eof()) {
            t = t1.get();
            
        }
    }
    return 0;
}


1
  • Замените ваше while (!t1.eof()) { t = t1.get(); на while(t1.get(t)) {, а то глаз режет...
    – Harry
    28 апр 2021 в 13:44

1 ответ 1

2

Я бы делал так - убедившись, что isalpha() дает true только для латинских букв:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream t1("file1.txt");
    ofstream t2("file2.txt");
    if (!t1 || !t2)
    { 
        cout << "Error of opening of a file ";
        return 0; 
    }
    char t;
    while (t1.get(t))
        if (!isalpha(t))
            t2.put(t);
}

В крайнем случае, написать такую функцию и самому очень просто:

bool isalpha(char t)
{
    return ('a' <= t && t <= 'z') || ('A' <= t && t <= 'Z');
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.