Ex5.3 feedback: use a string when reading from linebuffer
We do not know how long the words will be (except not longer than a line) so a std::string more suitable for this. Plus, we can now use instead of checking for a character ourselves.
This commit is contained in:
parent
b4edcd7ed1
commit
969ee37aa8
1 changed files with 3 additions and 5 deletions
|
@ -24,9 +24,6 @@ int main( int argc, char* argv[] ) {
|
||||||
const int bufsize = 100;
|
const int bufsize = 100;
|
||||||
char buf[bufsize];
|
char buf[bufsize];
|
||||||
|
|
||||||
const int linebufsize = 10;
|
|
||||||
char linebuf[linebufsize];
|
|
||||||
|
|
||||||
// if EOF, fh returns false
|
// if EOF, fh returns false
|
||||||
while( fh ) {
|
while( fh ) {
|
||||||
fh.getline(buf, bufsize);
|
fh.getline(buf, bufsize);
|
||||||
|
@ -36,10 +33,11 @@ int main( int argc, char* argv[] ) {
|
||||||
|
|
||||||
std::istringstream line(buf);
|
std::istringstream line(buf);
|
||||||
while( line ) {
|
while( line ) {
|
||||||
linebuf[0] = '\0';
|
std::string linebuf;
|
||||||
|
|
||||||
line >> linebuf;
|
line >> linebuf;
|
||||||
|
|
||||||
if ( linebuf[0] != '\0' ) {
|
if ( ! linebuf.empty() ) {
|
||||||
wordcount++ ;
|
wordcount++ ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue