views:

134

answers:

2

I need to write just a function that counts the number of integers in an already opened and good text file.

a. Assume there is a text file with a large number of integers divided by spaces

b. Write a function called analyzeFile that accepts a previously opened ifstream file object as a parameter, and counts the number of integers in the file.

c. It does not need to do anything with the integers, but it must count exactly the correct number of integers in the file and return that number to the calling function.

d. It also does not need to manipulate the file operations themselves, so it does not need to close the file or conduct any other actions other than counting the integers and returning the number of them.

Thank you for any help on my problem!

Edit: Here is what I have as a function do far, is it right, I don't know:

int analizeFile (ifstream &inf, const string &fileName) { 
   int count = 1; 
   int num; 
   fin.open(fileName.c_str() ); 
   fin >> num; 
   while (fin.good() ) { 
      fin>> num; 
      count ++; 
   } 
   return count;
} 
+2  A: 

Comments:

int analizeFile (ifstream &inf, const string &fileName) { 

Since the count is always a non-negative quantity, I'd prefer to use size_t rather than int. Nit: You may want to change the name of the function to analyzeFile.

int count = 1; 

Problem starts here: If your file does not have any integer then you return a wrong result.

 int num; 
   fin.open(fileName.c_str() ); 

No need to call open. This would typically be called by the ifstream ctor.

   fin >> num; 
   while (fin.good() ) { 

Again, this is not required. You can extract from the stream and test in the while condition -- something which is more frequently used.

  fin>> num; 
  count ++; 
   } 
   return count;
} 
dirkgently
+1 for taking the time and trouble to educate rather than just post a solution.
sgreeve
Whether or not this post *educated* the OP is certainly debatable. At this point in the OPs "curriculum," I believed that a clear, simple example would provide the most benefit. If the OP were ready to be learning about signed vs. unsigned integer types and constructors, it would have been apparent from the OP's question. Seeing a code snippet with the a priori knowledge of exactly what it does can be quite enlightening!
More importantly, the `fin.good()` is even wrong. Given this file content: "21", then if he initializes `count` to `0`, he will think that there are `0` numbers in the file (because `good()` returns false if the eof bit is set). On the other side if he initializes `count` to `1`, and the content is "21 ", then he will think there are two numbers since after reading 21, no eof or fail bit is set yet.
Johannes Schaub - litb
+1  A: 

You can use a functional approach too

// it was previously opened, so you don't need a filename. 
int analyzeFile (istream &inf) { 
   std::istream_iterator<int> b(inf), e;
   return std::distance(b, e);
}

If the iterator cannot read an integer, it will set the fail state on the stream and will compare equal to the end iterator. distance then returns the number of iteration steps it took to reach the end iterator.

Johannes Schaub - litb