arnuld <> wrote in message...
> This is the partial-program i wrote, as usual, i ran into problems
> halfway:
>
> /* C++ Primer - 4/e * Exercise 8.9 * STATEMENT:
> * write a function to open a file for input and then read its
> coontents into a vector of strings, storinig each line as separate
> element in vector.
> */
>
> #include <iostream>
> #include <fstream>
> #include <vector>
> #include <string>
>
> /* using Pointers because 1st argument is an input file-stream and
> for 2nd argument i want to take the original vector, not the copied
> one. next line is the source of error, line #22 */
> void read_file( ifstream* my_file, std::vector<std::string>* svec ){
> ifstream infile;
> infile.open("my_file");
> /* Process File Here */
> }
void read_file( std::string const &my_file, std::vector<std::string>
&svec ){
std::ifstream infile( my_file.c_str() );
if( not infile.is_open() ){ /* error */ }
/* Process File Here */
}
> int main(){
> std::vector<std::string> svec;
// > std::vector<std::string>* psvec;
>
> std::cout << "Enter the full Path to file: "; ifstream my_file;
// > ifstream* p_my_file;
// > std::cin >> p_my_file;
std::string p_my_file;
std::getline( std::cin, p_my_file );
// > read_file( p_my_file, psvec );
read_file( p_my_file, svec );
> return 0;
> }
If you really did want to do what you were thinking, don't limit your
function.
void read_file( std::istream &in, std::vector<std::string> &svec ){
// note: that is 'istream', not 'ifstream'.
for( std::string line; std::getline( in, line ); /*m_t*/ ){
svec.push_back( line );
} // for(line)
return;
} // read_file(istream&,vector<string>&)
int main(){
std::vector<std::string> svec;
std::ifstream infile( "MyFile.txt" );
std::istringstream instring( "This will \n simulate \n" ); // <sstream>
read_file( infile, svec );
read_file( instring, svec );
std::cout<<"Type in some lines"<<std::endl;
/* I'll leave it to you to figure a way to invoke/terminate
input on this one <G>*/
/* hint: ctrl-Z, ctrl-C, [return], as-is, ??? */
read_file( std::cin, svec );
std::copy( svec.begin(), svec.end(),
std:

stream_iterator<std::string>( std::cout, "\n" ) );
return 0;
} // main()
Don't complicate your programming with pointers, unless you are *forced* to.
:-}
--
Bob R
POVrookie