-1

I have a txt file called 'sample.txt' with data of the form

1.0

2.1

abc

4.2

I would like to extract the numbers 1.0, 2.1, 4.2 from the txt file and ignore the string 'abc'.

Before I extract the data, I would like to first count exactly how many numbers are there in the txt file.

Because I want to extract the numbers into a dynamic array and I need to know what size of array to create first.

My code for counting the number of numbers in the txt file is

ifstream myfile; 
myfile.open ("sample.txt"); 

int dataCount = 0;  // to count number of numbers

while (!myfile.eof())
{
    double x;         
    myfile >> x;    

    if (myfile.fail()) // no extraction took place
    {
        myfile.clear(); 
        myfile.ignore(numeric_limits<streamsize>::max(), '\n'); 
        continue;  // do nothing and start reading next data
    }

    // Count up if x successfully extracted
    dataCount++;
}

So now I am done counting the number of numbers but there is also nothing left in the ifstream myfile for me to extract. Is there anyway to count the numbers without extracting anything away from the ifstream myfile?

This is for a school assignment where I am not permitted to use a vector.

TaeNyFan
  • 109
  • 3
  • 1
    Use a vector instead of an array and it won't matter how many numbers are in the file. – Shawn Feb 14 '19 at 17:07
  • Can you not use `std::vector`? – NathanOliver Feb 14 '19 at 17:08
  • @Shawn This is for a school assignment and I am required to not use a vector – TaeNyFan Feb 14 '19 at 17:08
  • Also read this: https://latedev.wordpress.com/2012/12/04/all-about-eof/ – Shawn Feb 14 '19 at 17:09
  • 1
    If it's already a dynamic array, can't you start it at some small value and then once it's full, make a new larger dynamic array, transfer over the data from the old one and keep going (a-la vector push_back)? – scohe001 Feb 14 '19 at 17:09
  • 1
    https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – πάντα ῥεῖ Feb 14 '19 at 17:10
  • 1
    You can go back to the start of the file using the [`seekg()`](https://en.cppreference.com/w/cpp/io/basic_istream/seekg) function. – πάντα ῥεῖ Feb 14 '19 at 17:13
  • 2
    'I am not permitted to use vector'. Why? Are you being taught by a sadist? Ask your teacher what is the educational benefit of not being able to use the tools that are routinely employed by professional programmers. Why are you being forced to learn all this obscure pointer and memory handling when there are so many more useful topics you could be learning and experience you could be getting. Programming is hard enough to learn without wilfully making it harder. It's really unbelievable how bad the teaching of C++ is. – john Feb 14 '19 at 17:14
  • 1
    My advice (this is serious) would be to write your own basic vector class. Then you could use it for the rest of the course without breaking the rules. – john Feb 14 '19 at 17:17
  • 1
    @john I think it is indeed his intention for us to practice dynamic memory handling. And I think we get to start using vectors for the next assignment so it's not too bad. And if I use vectors for this I'll get zero marks. – TaeNyFan Feb 14 '19 at 17:23
  • @TaeNyFan Writing you r own vector class would involve dynamic memory management anyways (and doing so in an appropriate c++ way). – πάντα ῥεῖ Feb 14 '19 at 17:24
  • 1
    @TaeNyFan The thinking is wrong, because once you've learned vectors you'll not go back to dynamic memory handling. So the time is wasted. But I'm glad you are doing vectors next. – john Feb 14 '19 at 17:25
  • @πάνταῥεῖ oh, looks like I misunderstood john's comment. Thanks for the info. – TaeNyFan Feb 14 '19 at 17:26

1 Answers1

3

You can just close the file and open it up again. Or you could clear the end of file flag and seek back to the beginning.

myfile.clear();
myfile.seekg(0); // rewind the file to the beginning
john
  • 85,011
  • 4
  • 57
  • 81