How to check if string ends with .txt

后端 未结 11 1776
耶瑟儿~
耶瑟儿~ 2020-12-15 04:23

I am learning basic C++, and right now I have gotten a string from a user and I want to check if they typed the entire file name (including .txt) or not. I have the string,

11条回答
  •  一生所求
    2020-12-15 05:09

    you can just use another string to verify the extension like this :

    string fileName;
    
    cout << "Enter filename: \n";
    cin >> fileName;
    
    //string txt = fileName.Right(4);
    string ext="";
    for(int i = fileName.length()-1;i>fileName.length()-5;i--)
    {
        ext += fileName[i];
    }
    cout<

    checking if equals "txt." cause i starts with the length of the filename so ext is filled in the opposite way

提交回复
热议问题