Trouble with filestream and stringstream..
Can anyone tell me why the last number of input read from file is
printed three times in the below program.
Thanks..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
clock_t start = clock();
string line;
ifstream ifile("numbers.txt");
if(ifile.is_open())
{
int num;
while(!ifile.eof())
{
getline(ifile, line);
istringstream ss(line);
do
{
ss >> num;
cout << num << endl;
}while(ss.good());
}
ifile.close();
}
else
cout << "Unable to open file\n";
cout << ((clock() - start)/static_cast<double>(CLOCKS_PER_SEC) ) << '\n';
}
|
Why all these checks on ifile
? A single check while (ifile)
will do.
ok... that reduced count by one. but, still the last one is printed twice...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
string line;
ifstream ifile("numbers.txt");
int num;
while(ifile)
{
clock_t start = clock();
getline(ifile, line);
istringstream ss(line);
while(ss.good()) { ss >> num; cout << num << endl; }
}
ifile.close();
}
|
Last edited on
while(ss.good()) { ss >> num; cout << num << endl; }
Try change it from a while() loop to if() -
if(ss.good()) { ss >> num; cout << num << endl; }
I think, changing while loop to if may not work.
A sample input line is given below.
705 50 998 489 441 506 664 765 397 827 441 200 602 800 737 909 481 517 306 643 244 894 571 706 1 396 837 15 487 891 475 543 940 824 31 732 681 46 496 430 873 289 629 826 440 717 87 272 585 744 914 828 638 485 533 638 232 722 4 70 612 830 613 551 5 995 634 686 41 129 467 265 769 447 90 561 164 176 832 748 920 746 928 557 582 460 546 165 181 901 234 792 82 846 694 86 193 327 123 585 808 589 849 928 36 938 488 551 114 320 650 385 417 577 293 350 389 190 514 921 90 747 713 523 945 758 960 489 437 83 73 596 23 921 523 410 210 363 312 675 34 314 59 802 242 351 151 630 892 16 551 981 114 615 855 58 724 815 546 160 249 618 107 623 890 982 385 100 344 696 774 377 361 185 530 603 887 680 232 779 47 782 111 160 748 966 218 472 132 115 983 380 85 90 2 326 423 386 777 766
|
Like this there are a number of lines. So, if i change
to
, it may give only one number from each line. And in my case, last number,(for this example input, 766) is printed twice.
Ahh.. my bad.. I've tried tampering with it again and with 2 lines of your example lines.. I came up with this -
1 2 3 4 5 6 7 8 9 10 11 12 13
|
if (ifile.is_open()) //if it's open
{
clock_t start = clock(); //start clock
while(getline(ifile, line)) //while getline
{
stringstream ss(line);
while (ss >> num) //if stringstream can load onto number
{
std::cout << num << std::endl; //print
}
}
ifile.close(); //close
}
|
It appears to be working fine for me.. tell me how it works for you?
The stream doesn't know it's bad until you've performed the read.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
int main()
{
std::ifstream ifile("C:\\temp\\numbers.txt");
std::string line;
while (std::getline(ifile, line))
{
std::istringstream ss(line);
int n;
while (ss >> n)
{
std::cout << n << std::endl;
}
}
return 0;
}
|
Thanks... this works...
now i got one more doubt. when will we use
if
is enough to check whether there is more input to read.
The problem is yours fails if there's more stuff in the stream, but no more numbers, like a space at the end of the line.
Topic archived. No new replies allowed.