How to check for file opened

Hello, so i need to check, if certain file exists, first i check if the file has no dot, then if the extension is .txt or .dat, and then i need to check if the provided .txt file exists,
all good until it gets into the check for the opened file. it keeps looping even when im providing the correct file name.
The txt file that should be correct is one named dataFile3.txt
thanks

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
 #include<string>
#include<iostream>
#include <fstream>

using namespace std;

int main(){
    ifstream inFile;
    cout<<"Enter file Name ";
    string  fileName;
    cin>>fileName;
    size_t found = fileName.find(".");

    if(found == string :: npos) {
            cout<< "Error, invalid file name."<<endl;
            cout << "Enter Input File Name: ";
            cin >>fileName;
        while (!(fileName.substr(fileName.size() - 3) == "dat" || fileName.substr(fileName.size() - 3) == "txt")){
            cout << "Error, file name must be '.txt' or '.dat' extension."<<endl;
            cout << "Enter Input File Name: ";
            cin >>fileName;
            }

        while(!inFile.is_open()){

            cout << "setFileName: Error, file does not exist."<<endl;
            cout << "Enter Input File Name: ";
            cin >>fileName;
        }
    }

    else {
        while (!(fileName.substr(fileName.size() - 3) == "dat" || fileName.substr(fileName.size() - 3) == "txt")){
        cout << "Error, file name must be '.txt' or '.dat' extension."<<endl;
        cout << "Enter Input File Name: ";
        cin >>fileName;
        }
         while(!inFile.is_open()){

            cout << "setFileName: Error, file does not exist."<<endl;
            cout << "Enter Input File Name: ";
            cin >>fileName;
        }

    }

}
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ifstream fin;

    while (true)
    {
        cout << "Enter file Name ";
        string filename;
        cin >> filename;
        auto dot = filename.rfind('.');
        if (dot != filename.npos)
        {
            auto substr = filename.substr(dot + 1);
            if (substr == "dat" || substr == "txt")
            {
                fin.open(filename);
                if (fin)
                    break;
                cout << "error: the file does not exist\n";
                continue;
            }
        }
        cout << "error: the filename must end in .txt or .dat\n";
    }

    cout << "file is open\n";
}

Last edited on
Thanks a lot ,that logic is way better, i adapted it to my code and came up with the exact requisites my assignment was asking me for
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

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

int main()
{
 while(true){
            cout << "Enter Input File Name: ";
            cin >>fileName;
            ifstream inFile(fileName);
            size_t found = fileName.find(".");
            if(found != string :: npos) {
                if ((fileName.substr(fileName.size() - 3) == "dat" || fileName.substr(fileName.size() - 3) == "txt")){
                    if (inFile.is_open())
                    return true;
                cout<<"Error, file does not exist."<<endl;
                continue;
            }
            cout << "Error, file name must be '.txt' or '.dat' extension."<<endl;
        }

        else {
            cout <<"Error, invalid file name."<<endl;
            continue;
        }

    }
}
Last edited on
Your fucked up spacing makes it a little hard to read, but I think you've messed it up a little there.

In the code I posted I used find where I should have used rfind, so I made that edit.

I have no idea what you're trying to actually do with this code. I assumed it was a snippet to actually open a file for use.
Topic archived. No new replies allowed.