Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 23rd, 2005, 05:51 AM
Dave Reid
Guest
 
Posts: n/a
Default Reading file input into a string vector

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
  #2  
Old July 23rd, 2005, 05:51 AM
Shezan Baig
Guest
 
Posts: n/a
Default Re: Reading file input into a string vector



Dave Reid wrote:[color=blue]
> 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];[/color]



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[color=blue]
> 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[/color]

  #3  
Old July 23rd, 2005, 05:51 AM
Dave Reid
Guest
 
Posts: n/a
Default Re: Reading file input into a string vector

"Shezan Baig" <shezanbaig2004@gmail.com> wrote in
news:1117146258.806797.72380@o13g2000cwo.googlegro ups.com:
[color=blue][color=green]
>> char buff2[1000][256];[/color]
>
>
>
> 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.[/color]

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
  #4  
Old July 23rd, 2005, 05:51 AM
Peter Julian
Guest
 
Posts: n/a
Default Re: Reading file input into a string vector


"Dave Reid" <ddreid@comcast.net> wrote in message
news:d75hf9$ebe$1@gnus01.u.washington.edu...[color=blue]
> 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[/color]

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 !!).

  #5  
Old July 23rd, 2005, 05:51 AM
Larry I Smith
Guest
 
Posts: n/a
Default Re: Reading file input into a string vector

Dave Reid wrote:[color=blue]
> "Shezan Baig" <shezanbaig2004@gmail.com> wrote in
> news:1117146258.806797.72380@o13g2000cwo.googlegro ups.com:
>[color=green][color=darkred]
>>> char buff2[1000][256];[/color]
>>
>>
>>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.[/color]
>
> 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[/color]

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.
  #6  
Old July 23rd, 2005, 05:51 AM
Old Wolf
Guest
 
Posts: n/a
Default Re: Reading file input into a string vector

Dave Reid wrote:[color=blue]
> 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.[/color]

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:
[color=blue]
>
> int main() {
> string str1("booger");
> string str2("test");
> int index = 0;
> char buffer[256]; //two dimensional string array[/color]

I hope you know that that was not a 2-d string array!
[color=blue]
> char buff2[1000][256];
> ifstream examplefile ("example.txt");
> if (! examplefile.is_open())
> { cout << "Error opening file"; exit (1); }[/color]

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.
[color=blue]
> cout << str1 << "\n\n";
> while (! examplefile.eof() ) //do until end of file is reached[/color]

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.
[color=blue]
> {
> examplefile.getline (buff2[index],100);[/color]

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?)
[color=blue]
> cout << "buffer:" << buff2[index] << endl;
> index = index + 1;[/color]

What about when index exceeds 1000?
[color=blue]
> }
> return 0;
> }
>[/color]

  #7  
Old July 23rd, 2005, 05:51 AM
Jon Bell
Guest
 
Posts: n/a
Default Re: Reading file input into a string vector

In article <d75hf9$ebe$1@gnus01.u.washington.edu>,
Dave Reid <ddreid@comcast.net> wrote:[color=blue]
>
>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.[/color]

#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 <jtbell@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles