By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,099 Members | 2,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,099 IT Pros & Developers. It's quick & easy.

Reading file input into a string vector

P: n/a
Hi everyone...
I'm pretty much a newbie C++ user, and I've run into a problem.
I'm trying to read in a large text file, and then do manipulations on
it. I can read it into a large 2-dimensional character array, but I'd
really like to read it into a vector of strings. Here's how I'm doing
the read into the char array:

int main() {
string str1("booger");
string str2("test");
int index = 0;
char buffer[256]; //two dimensional string
array
char buff2[1000][256]; // 1K lines, 256
chars each
ifstream examplefile ("example.txt"); //test file
if (! examplefile.is_open()) //check to make
sure file can be opened
{ cout << "Error opening file"; exit (1); }
cout << str1 << "\n\n";
while (! examplefile.eof() ) //do until end of
file is reached
{
examplefile.getline (buff2[index],100); //get the
line, put into buffer
cout << "buffer:" << buff2[index] << endl; //print out
the contents of buffer
index = index + 1;
}
return 0;
}

So that works out great...but I'm not sure how I'd modify that to
replace the char buffer[] with a string vector. Any hints for me?

Thanks in advance...

dave
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a


Dave Reid wrote:
Hi everyone...
I'm pretty much a newbie C++ user, and I've run into a problem.
I'm trying to read in a large text file, and then do manipulations on
it. I can read it into a large 2-dimensional character array, but I'd
really like to read it into a vector of strings. Here's how I'm doing
the read into the char array:

int main() {
string str1("booger");
string str2("test");
int index = 0;
char buffer[256]; //two dimensional string
array
char buff2[1000][256];
You just need to replace this with:

std::vector<std::string> buff2(1000);

The rest of your code shouldn't need to change, unless i mised
something.

hope this hlpes,
-shez-


// 1K lines, 256 chars each
ifstream examplefile ("example.txt"); //test file
if (! examplefile.is_open()) //check to make
sure file can be opened
{ cout << "Error opening file"; exit (1); }
cout << str1 << "\n\n";
while (! examplefile.eof() ) //do until end of
file is reached
{
examplefile.getline (buff2[index],100); //get the
line, put into buffer
cout << "buffer:" << buff2[index] << endl; //print out
the contents of buffer
index = index + 1;
}
return 0;
}

So that works out great...but I'm not sure how I'd modify that to
replace the char buffer[] with a string vector. Any hints for me?

Thanks in advance...

dave


Jul 23 '05 #2

P: n/a
"Shezan Baig" <sh************@gmail.com> wrote in
news:11*********************@o13g2000cwo.googlegro ups.com:
char buff2[1000][256];


You just need to replace this with:

std::vector<std::string> buff2(1000);

The rest of your code shouldn't need to change, unless i mised
something.


I'd already tried that...and I get compilation errors.

]$ g++ readfile.cc
readfile.cc: In function `int main ()':
readfile.cc:22: no matching function for call to `ifstream::getline
(basic_string<char, string_char_traits<char>,
__default_alloc_template<true, 0> > &, int)'
/usr/include/g++-3/iostream.h:129: candidates are: istream
&istream::getline (char *, int, char = '\n')
/usr/include/g++-3/iostream.h:131: istream
&istream::getline (unsigned char *, int, char = '\n')
/usr/include/g++-3/iostream.h:136: istream
&istream::getline (signed char *, int, char = '\n')

dave
Jul 23 '05 #3

P: n/a

"Dave Reid" <dd****@comcast.net> wrote in message
news:d7**********@gnus01.u.washington.edu...
Hi everyone...
I'm pretty much a newbie C++ user, and I've run into a problem.
I'm trying to read in a large text file, and then do manipulations on
it. I can read it into a large 2-dimensional character array, but I'd
really like to read it into a vector of strings. Here's how I'm doing
the read into the char array:

int main() {
string str1("booger");
string str2("test");
int index = 0;
char buffer[256]; //two dimensional string
array
char buff2[1000][256]; // 1K lines, 256
chars each
ifstream examplefile ("example.txt"); //test file
if (! examplefile.is_open()) //check to make
sure file can be opened
{ cout << "Error opening file"; exit (1); }
cout << str1 << "\n\n";
while (! examplefile.eof() ) //do until end of
file is reached
{
examplefile.getline (buff2[index],100); //get the
line, put into buffer
cout << "buffer:" << buff2[index] << endl; //print out
the contents of buffer
index = index + 1;
}
return 0;
}

So that works out great...but I'm not sure how I'd modify that to
replace the char buffer[] with a string vector. Any hints for me?

Thanks in advance...

dave


Write a class that does this for you. Lets call it FileParser:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <iterator>

class FileParser
{
// members
std::string s_filename;
std::string s_buffer;
std::ifstream ifs;
std::vector<std::string> vs;
public:
// ctor and d~tor
FileParser(std::string s) : s_filename(s), s_buffer(), ifs(), vs() { }
~FileParser() { }

void read( )
{
ifs.open(s_filename.c_str(), std::ios::in);
if (!ifs)
{
// todo: std::cout an open_fstream_error or throw exception
}
while ( std::getline( ifs, s_buffer ) )
{
vs.push_back(s_buffer);
}
if (!ifs.eof()) // if reason of termination != eof
{
// todo: std::cout a read_fstream_error or throw exception
}
} // read()

void display() const
{
std::copy( vs.begin(),
vs.end(),
std::ostream_iterator<std::string>( std::cout,
"\n" ) );
} // display

}; // class FileParser

int main()
{
FileParser fileparser("data.dat"); // todo: specify your target
fileparser.read();

// you may need to parse the vector
// if what you want is words, not lines
// do it in the class with a member function
// (hint: #include <sstream> and use a std::stringstream)

fileparser.display();

return 0;
}

By the way, the read()'s procedure:

a) open file and check for error. (note: if(!ifs.good()) {...} is ok as
well)
b) loop std::getline until it fails
c) verify if fstream failure was not an eof error
d) no need to ifs.close() unless you need to open it again

is standard practice. That i owe to the extraordinary ppl that populate this
newsgroup (thanks !!).

Jul 23 '05 #4

P: n/a
Dave Reid wrote:
"Shezan Baig" <sh************@gmail.com> wrote in
news:11*********************@o13g2000cwo.googlegro ups.com:
char buff2[1000][256];

You just need to replace this with:

std::vector<std::string> buff2(1000);

The rest of your code shouldn't need to change, unless i mised
something.


I'd already tried that...and I get compilation errors.

]$ g++ readfile.cc
readfile.cc: In function `int main ()':
readfile.cc:22: no matching function for call to `ifstream::getline
(basic_string<char, string_char_traits<char>,
__default_alloc_template<true, 0> > &, int)'
/usr/include/g++-3/iostream.h:129: candidates are: istream
&istream::getline (char *, int, char = '\n')
/usr/include/g++-3/iostream.h:131: istream
&istream::getline (unsigned char *, int, char = '\n')
/usr/include/g++-3/iostream.h:136: istream
&istream::getline (signed char *, int, char = '\n')

dave


Use the getline from <string>. See the STL docs for details:

std::getline(some_ifstream, some_std_string, some_OPTIONAL_delimiter)

e.g.:

// get everything from 'examplefile' up to the next newline
// into the std::string 'buff2[index]'. the newline is dropped.
std::getline(examplefile, buff2[index]);

// get everything from 'examplefile' up to the next SPACE char
// into the std::string 'buff2[index]'. the SPACE char is dropped.
std::getline(examplefile, buff2[index], ' ');

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #5

P: n/a
Dave Reid wrote:
Hi everyone...
I'm pretty much a newbie C++ user, and I've run into a problem.
I'm trying to read in a large text file, and then do manipulations on
it. I can read it into a large 2-dimensional character array, but I'd
really like to read it into a vector of strings.
The vector version is far easier:

std::vector<std::string> vec;
std::string s;
while ( std::getline(examplefile, s) )
vec.push_back(s);

The main point here is std::getline which is used for getting
a std::string from a stream, whereas istream::getline is used for
getting characters up to a certain length etc. as you found out.

FWIW I'll give you some advice on your original code:

int main() {
string str1("booger");
string str2("test");
int index = 0;
char buffer[256]; //two dimensional string array
I hope you know that that was not a 2-d string array!
char buff2[1000][256];
ifstream examplefile ("example.txt");
if (! examplefile.is_open())
{ cout << "Error opening file"; exit (1); }
It's good to avoid exit() in C++, because it won't destroy
any objects correctly (eg. examplefile won't be destroyed).
Instead, return a value from main, or throw an exception.
cout << str1 << "\n\n";
while (! examplefile.eof() ) //do until end of file is reached
But what if end of file is reached during the getline
call below? Then your program will operate on garbage until
it gets around to the start of the loop again.

The correct technique is to check the getline call itself for failure.
{
examplefile.getline (buff2[index],100);
I guess you know that this will break up any lines that are
longer than 99 characters. (NB. buff2 has lines of size
256, why are you reading 100?)
cout << "buffer:" << buff2[index] << endl;
index = index + 1;
What about when index exceeds 1000?
}
return 0;
}


Jul 23 '05 #6

P: n/a
In article <d7**********@gnus01.u.washington.edu>,
Dave Reid <dd****@comcast.net> wrote:

I'm trying to read in a large text file, and then do manipulations on
it. I can read it into a large 2-dimensional character array, but I'd
really like to read it into a vector of strings.


#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main ()
{
// Declare the vector with zero size initially and let it grow as needed.

vector<string> linevec;
string line;
ifstream infile ("foo.txt");

// Read the file one line at a time. The loop terminates when it tries to
// read past the end of the file. push_back() extends the vector by one
// position and copies the given string into the newly-created position.
// This way, you don't have to worry about how big to make the vector
// initially, or about what to do if the vector overflows.

// NOTE: It is almost never correct to use eof() to control an input
// loop, because it becomes true only *after* you have tried to read past
// the end of file and *failed*.

while (getline (infile, line))
{
linevec.push_back (line);
}

// See what we've read in.

for (int k = 0; k < linevec.size(); ++k)
{
cout << linevec[k] << endl;
}

return 0;
}

--
Jon Bell <jt****@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.