How to read a user-given file

I want to ask the user for the name of a text file and then have the file read into a vector which I will then sort. My question is: How do I read the file through a user-given statement? For example, the user will type "words.txt" and that file will load. This is what I have so far

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

using namespace std;

int main()
{
    string word;
    vector<string> words;
    ifstream readFile("input.txt");
    while(getline(readFile, word, '\n'))
        words.push_back(word);
    readFile.close();
    sort(words.begin(), words.end());

    cout << "Vector contains:";
    for (int i=0; i<words.size(); i++)
    {
           cout << " " << words.at(i);
    }

return 0;
}



So, I want to have the user name where "input.txt" is. Thanks.
Last edited on
I'll help you out. Lets look it over. You wish to have a person input a name/string/w.e you want to call it, and then use that information to open a file corresponding with that.
Well think it over, just get the persons input first store it into a variable then create your ifstream object with the variable that the person inputted.
I was thinking along those lines. Here is what I tried

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

using namespace std;

int main()
{
    
    string x;
    cout << "file to be stored?";
    cin >> x;
    
    string word;
    vector<string> words;
    
    ifstream readFile(x);
    while(getline(readFile, word, '\n'))
        words.push_back(word);
    readFile.close();
    sort(words.begin(), words.end());

    cout << "Vector contains:";
    for (int i=0; i<words.size(); i++)
    {
           cout << " " << words.at(i);
    }

return 0;
}


I tried to store the user-given name as string x and then use ifstream readFile(x) but it doesn't like that.
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>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    
    string x;
    cout << "file to be stored?";
    cin >> x;
    
    string word;
    vector<string> words;
    
    ifstream readFile(x.c_str());
    while(getline(readFile, word, '\n'))
        words.push_back(word);
    readFile.close();
    sort(words.begin(), words.end());

    cout << "Vector contains:";
    for (int i=0; i<words.size(); i++)
    {
           cout << " " << words.at(i);
    }

return 0;
}


Hmm I think that should fix it. If I'm not mistaken it only accepts character constants... And we are giving it a string... I'm not quite sure how that works out.
Last edited on
Thanks a lot. Can you explain to me what .c_str() does?
http://www.cplusplus.com/reference/string/string/c_str/

That's as far as I know about c_str().
But I do understand that c_str() returns a pointer array of chars equivalent to the string given it(there's more to it though), which is why the constructor of ifstream can handle.

But what I have figured out is that data() works just as fine as c_str().

The thing about data() is that it returns a pointer array of characters equivalent to the string... Which the ifstream constructor can handle.
Last edited on
It takes the std::string and converts it to a const char*, commonly called a C-string because that's how strings are represented in C.
Ok, thanks. I understand the concept but not so much the details lol
Topic archived. No new replies allowed.