A
arnuld
This works fine, I welcome any views/advices/coding-practices
/* C++ Primer - 4/e
*
* Exercise 8.9
* STATEMENT:
* write a program to store each line from a file into a
* vector<string>. Now, use istringstream to read read each line
* from the vector a word at a time.
*
*/
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
void read_file( std::ifstream& infile, std::vector<std::string>&
svec )
{
std::string a_line;
while( std::getline( infile, a_line ) )
{
svec.push_back( a_line );
}
}
/* This program works nearly in 3 steps:
step 1 : we take the path of the file and open it.
step 2 : we read the file into the vector<string>
ansd close it.
step 3 : we use the istringstream to read each line,
a word at a time.
*/
int main()
{
/* step 1 */
std::vector<std::string> svec;
std::cout << "Enter full path of the file: ";
std::string path_to_file;
std::getline( std::cin, path_to_file );
std::ifstream infile( path_to_file.c_str() );
/* step 2 */
/* check whether file was even opened or not */
if( infile.good() )
{
read_file( infile, svec );
}
/* reading finished, don't forget to close the file */
infile.close();
/* step 3 */
for( std::vector<std::string>::const_iterator iter = svec.begin();
iter != svec.end(); ++iter)
{
std::string a_word;
/* bind that line, to the istringstream */
std::istringstream vector_line( *iter );
while( vector_line >> a_word )
{
std::cout << a_word << " ";
}
}
std::cout << std::endl;
return 0;
}
--------- OUTPUT ------------
[arnuld@Arch64 c++]$ g++ -ansi -pedantic -Wall -Wextra ex_08-16.cpp
[arnuld@Arch64 c++]$ ./a.out
Enter full path of the file: /home/arnuld/programming/scratch.txt
;; This buffer is for notes you don't want to save, and for Lisp
evaluation. ;; If you want to create a file, visit that file with C-x
C-f, ;; then enter the text in that file's own buffer.
[arnuld@Arch64 c++]$
/* C++ Primer - 4/e
*
* Exercise 8.9
* STATEMENT:
* write a program to store each line from a file into a
* vector<string>. Now, use istringstream to read read each line
* from the vector a word at a time.
*
*/
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
void read_file( std::ifstream& infile, std::vector<std::string>&
svec )
{
std::string a_line;
while( std::getline( infile, a_line ) )
{
svec.push_back( a_line );
}
}
/* This program works nearly in 3 steps:
step 1 : we take the path of the file and open it.
step 2 : we read the file into the vector<string>
ansd close it.
step 3 : we use the istringstream to read each line,
a word at a time.
*/
int main()
{
/* step 1 */
std::vector<std::string> svec;
std::cout << "Enter full path of the file: ";
std::string path_to_file;
std::getline( std::cin, path_to_file );
std::ifstream infile( path_to_file.c_str() );
/* step 2 */
/* check whether file was even opened or not */
if( infile.good() )
{
read_file( infile, svec );
}
/* reading finished, don't forget to close the file */
infile.close();
/* step 3 */
for( std::vector<std::string>::const_iterator iter = svec.begin();
iter != svec.end(); ++iter)
{
std::string a_word;
/* bind that line, to the istringstream */
std::istringstream vector_line( *iter );
while( vector_line >> a_word )
{
std::cout << a_word << " ";
}
}
std::cout << std::endl;
return 0;
}
--------- OUTPUT ------------
[arnuld@Arch64 c++]$ g++ -ansi -pedantic -Wall -Wextra ex_08-16.cpp
[arnuld@Arch64 c++]$ ./a.out
Enter full path of the file: /home/arnuld/programming/scratch.txt
;; This buffer is for notes you don't want to save, and for Lisp
evaluation. ;; If you want to create a file, visit that file with C-x
C-f, ;; then enter the text in that file's own buffer.
[arnuld@Arch64 c++]$