Enter manually a path in C++

Hi guys, i would like an help about putting manually a path in my code.

This is what my code should looks like


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

int main ()
//declare what the path file looks like (char and not, for ex: C:\test\testfile.text) as we can se there aren't only char type inputs but also "/"
cin >> path;
ifstream f (path);
string s;

if (!f)

{
	cout << "the file doesn't exist! Try moving the exe file or put the txt file into this folder!" << endl;
}


while(f.good()) 
    {
        getline(f, s);
        cout<<s<<endl;
    }
    f.close(); 

    return 0;
}


any help please :)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <fstream>
#include <iostream> // **** EDIT: this was missing earlier **** 

int main()
{
    std::string path ;
    std::cout << "path? " ;

    // http://www.cplusplus.com/reference/string/string/getline/
    std::getline( std::cin, path ) ; // paths may contain whitespace

    std::ifstream file(path) ;
    if( !file.is_open() )
    {
        std::cerr << "failed to open input file '" << path << "'\n" ;
        return 1 ;
    }

    // use file
}
Last edited on
thx a lot for the help :D
Topic archived. No new replies allowed.