Reading a text file and inserting it into a vector - HELP

I'm attempting to write a C++ program that takes input from the user - 2 strings and puts them in a vector,that part works, I'm also able to save the information to a text file and display it to the screen. But when I attempt to read the text file and re-enter it into a vector then display it...well lets just say that things go down hill quickly. I'm hoping that someone would be kind enough to point me in the right direction.
For the "loadData" function here's what I have that isn't working.
1
2
3
4
5
6
7
8
9
10
11
void data::loadData()
{
    ifstream Dfile;
    Dfile.open("/home/Data.txt");
    while(!"Data.txt" == EOF)
    {
        Dfile.getline("Data.txt",80);
        Data.push_back("Data.txt");
    }
    Dfile.close();
}

Last edited on
while(!"Data.txt" == EOF) is just a string, this line of code references nothing. You would need to use
 
while(!Dfile.eof())


actually, you do this for all of your variables. "Data.txt" is just a string, Dfile is the object you want to use, replacing all of your data.txt's with Dfile should work.
Note** you might want to error check the file before pushing it into a vector, that could give you some crazy head aches later in the program if you dont check the file(either for being open or the data it holds).
Dfile is the object you want to use, replacing all of your data.txt's with Dfile should work.

Thank you for your quick reply, I think I made the changes that you suggested, but if I did it correctly I'm still getting the following error messages.
home/bruce/C++/Contacts/data.cpp: In member function ‘void data::loadData()’:
/home/bruce/C++/Contacts/data.cpp:49:28: error: no matching function for call to ‘std::basic_ifstream<char>::getline(std::ifstream&)’
/home/bruce/C++/Contacts/data.cpp:49:28: note: candidates are:
/usr/include/c++/4.6/istream:599:5: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize, std::basic_istream<_CharT, _Traits>::char_type) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::char_type = char, std::streamsize = int]
/usr/include/c++/4.6/istream:599:5: note: candidate expects 3 arguments, 1 provided
/usr/include/c++/4.6/istream:408:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::getline(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>, std::basic_istream<_CharT, _Traits>::char_type = char, std::streamsize = int]
/usr/include/c++/4.6/istream:408:7: note: candidate expects 2 arguments, 1 provided
/home/bruce/C++/Contacts/data.cpp:50:29: error: no matching function for call to ‘std::vector<std::basic_string<char> >::push_back(std::ifstream&)’
/home/bruce/C++/Contacts/data.cpp:50:29: note: candidate is:
/usr/include/c++/4.6/bits/stl_vector.h:826:7: note: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_string<char> >, std::vector<_Tp, _Alloc>::value_type = std::basic_string<char>]
/usr/include/c++/4.6/bits/stl_vector.h:826:7: note: no known conversion for argument 1 from ‘std::ifstream {aka std::basic_ifstream<char>}’ to ‘const value_type& {aka const std::basic_string<char>&}’


1
2
3
4
5
6
7
8
9
10
11
void data::loadData()
{
    ifstream Dfile;
    Dfile.open("/home/Data.txt");
    while(!Dfile.eof())
    {
        Dfile.getline(Dfile);
        Data.push_back(Dfile);
    }
    Dfile.close();
}


I think it's telling me that line 7 (in this case) expects 3 arguments but is only getting 1
I'm of the opinion that I haven't followed your instructions the way you intended.
Note** you might want to error check the file before pushing it into a vector, that could give you some crazy head aches later in the program if you dont check the file(either for being open or the data it holds).


You're absolutely correct, I just haven't gotten that far yet...I'm still struggling with trying to get the strings from the text file into the vector...lol
You need to read from the file into a buffer, and then add the contents of the buffer to the vector. See this page for further info + an example.

istream::getline
http://www.cplusplus.com/reference/iostream/istream/getline/
Last edited on
Thank you for the answer and the link.
Thanks to both of you...my program now works the way I want it to.
Topic archived. No new replies allowed.