Hi,

I need suggestions on how to count the words, letters, characters on a file . I created a program of it however, when i use the "seekp(0L,ios::beg);", it doesn't go to the beginning. I want to just close and re-open the file but I think there is a better way than that. And also, how to count the vowels on a file?

Thank you so much.

This is the program i created:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main ()
{
    string words, vowels;
    char characters, letters;
    int count1=0, count2=0, count3=0, count4=0;
    
    cout << "Welcome!" << endl;
    
    ifstream Preamble("Preamble.txt");
    
if (Preamble.fail())
cout << "File Doesn't Exist. Check the folder." << endl;
  
  
for (;Preamble.good(), ; count1++ )
    Preamble >> words ;
    
                                Preamble.seekg(0,ios::beg);
                                
for (;Preamble.good(); count2++ )      
    Preamble >> characters ;  
    
                                Preamble.seekg(0,ios::beg);  
                                
for (;Preamble.good(); count3++)
    Preamble.get(letters);       
   
                                Preamble.seekg(0L,ios::beg);                          
   
cout << "Number of words : " << count1 << endl;
cout << "Number of characters : " << count2 << endl;
cout << "Number of letters : " << count3 << endl;    
cout << "Number of vowels : " << count4 << endl;    
      
        Preamble.close();
        
        system ("pause");
        return 0;
        
        }

I need suggestions on how to count the words, letters, characters on a file . I created a program of it however, when i use the "seekp(0L,ios::beg);", it doesn't go to the beginning. I want to just close and re-open the file but I think there is a better way than that.

Why would there be a better way? Just close and open.

so there is no other way? i guess i'll just open close it

Sure there are other ways that are more complex and confusing. But why not just do the simple way. It's maintainable and understandable.

This article has been dead for over six months. Start a new discussion instead.