Hi,

I have a text file with characters. I must able to read the length of characters in the file by every line and count the frequency of each characters for each line. I have tried on writing this program but this does not work. Can anyone help me on this? Thank you in advance.

int main()
{
	const int MAXLENGTH=10;
	const int MAXCHARS=1000;
	static double i[26];
	char filename[MAXLENGTH]="ex.txt";
	char descrip[MAXCHARS];
	char str2[26]= {'A','B','C', 'D', 'E', 'F', 'G', 'H', 'I','J', 'K', 'L', 'M', 'N','O', 'P', 'Q', 'R', 'S', 'T','U', 'V', 'W','X' ,' Y','Z'};
	int ch,count,z,q;
	
	ifstream infile;


	infile.open(filename, ios::in);

	if(infile.fail())
	{
		cout<<"The file was not successfully opened"<<endl;
		getch();
		exit(1);
	}

	
	while((ch=infile.get())!=EOF)
	{
		count = strlen(descrip);
		cout<<"\nThe length of the string is: "<<count<<endl;

		for(z = 0; z < count; z++) 
		{
		   for(q=0; q < 27 ; q++)
			{
				if( descrip[z]== str2[q])				
				i[q]++;				
			}   
		}

		for(q=0; q < 26; q++)
		{
			double g;

		  	g=i[q]/count;
			cout<<g<<endl;
		}
	}
	infile.close();
	cout<<endl;

	getch();
	return 0;
}
Member Avatar for JSPMA1988

First, make sure your text file is in the same directory as your source file, otherwise you need to specify the full file path.

Second:

infile.open(filename, ios::in);

should just be

infile.open(filename);

Third, your while statement condition needs to look like this (although this has given me errors in the past):

while(infile >> ch)

Those are the three things I see that could be causing the problem right now.

All characters are also integer values. A=65, B=66, a=97, SPACE=32, etc. Do a search for ASCII CHART to see all the characters and their value.

With that in mind, you can use the character (value) as an index into an array. Each character increments the array element associated with it. There you have a frequency table for all characters in the file.

I am still new for c++. Hope you all can help me as I am having a
very limited time to solve it out.

JSPMA1988: Well i have tried as what you have said but there is no any output.
WaltP: Can you please elaborate more on it.

Do you understand arrays? Indexes? What more needs to be said?

If you have a specific problem, ask a specific question and show us the problem area. Your mechanic won't fix your car if it's in your garage, will he? We can't help with your code if all you tell us is "Well i have tried as what you have said but there is no any output."

Personal suggestion: take your time to study at least the basics of c++.
Here are just a few hints

Instead of using

while((ch=infile.get())!=EOF)

use this

std::string str;
while (getline(infile, str)) {
//Now "str" contains the entire line
//...

As for the character couting, try something like this

#include <algorithm> //Important!
...
std::string line; //Already filled
char chr; //The character you're looking for
int chrs = count(line.begin(), line.end(), chr);

That's all!

Why read an entire line and step through each character when you can read each character and use it?

As for your last suggestion, how does it accomplish "count the frequency of each characters"?

That's all...

I have a text file with characters. I must able to read the length of characters in the file by every line and count the frequency of each characters for each line.
I am still new for c++.

Simplify the problem by breaking it up into two smaller sub-problems:

a. Read each line in a file

b. Count the frequency of each character in a single line

And now, a. Read each line in a file can be broken up into

a1. Open the file for input
a2. Read line by line till end of file


a1. Open the file for input is easy:

std::ifstream file( filename ) ;
if( !file )
{
   std::cerr << "error opening file\n" ;
   std::exit( EXIT_FAILURE ) ;
   // or if this is in main() return EXIT_FAILURE ;
}

a2. Read line by line till end of file
The easiest and least error-prone way to do this is to use a std::string.
See this tutorial: http://www.arachnoid.com/cpptutor/student2.html

std::string line ;
while( std::getline( file, line ) )
{
    // b. Count the frequency of each character in the line
}

Now the problem reduces to:
1. We have a variable line of type std::string containing the line just read from the file.
2. The number of chars in the line is given by line.size(). Store this in a variable N.
3. The chars are line[0], line[1], ... line[N-1].

We need to count the frequency of each character in the line.

Take it up from there.

Why read an entire line and step through each character when you can read each character and use it?

I think I haven't stepped through each character at all.

As for your last suggestion, how does it accomplish "count the frequency of each characters"?

I have shown a way to count the occurrences of a character in a string. Pretty clear to me, it's up to the OP to adapt it.

That's all!

I think I haven't stepped through each character at all.

You would have to in order to get the frequency information.

You would have to in order to get the frequency information.

Why not using the count method I posted? That requires stepping through each alphabet character, not every line character.

I see what you are saying. However, I believe you'd have to "count()" over the line 26 times to get an array of letter counts. We don't even know if the OP can use the STL for this assignment. I agree with your getline approach, but when it comes to the individual lines the approach that WaltP and vijayan are advocating would probably serve the OP better.

Hi all,

Basically here I have a text file that contain alphabet such as

AAHUJIKKKL LENHYUIJNN
KOLPJIHUGY FTDNNJJJ

1- So, here I should able to count the number of line which are 2.
2- Next, I should able to count the length in each line which are 20 for the first line and 18 for the second line.
3- Further, I should able to know the number of occurrence for each alphabet.
4- For example the number of occurrence of A for the first line is 2. Therefore 2/20 gives the proportions for alphabet A.

Hi,

Vijayan121:

Now, I could read the total lines in the text file but when i tried to read each lines to get the proportions of the alphabet, I can't.

int main()
{
	const int MAXLENGTH=10;
	const int MAXCHARS=1000;
	static double i[26];
	char filename[MAXLENGTH]="ex.txt";
	char descrip[MAXCHARS];
	char str2[26]= {'A','B','C', 'D', 'E', 'F', 'G', 'H', 'I','J', 'K', 'L', 'M', 'N','O', 'P', 'Q', 'R', 'S', 'T','U', 'V', 'W','X' ,' Y','Z'};
	int ch,count,z,q;
        char c;
	
	ifstream infile;


	infile.open(filename, ios::in);

	if(infile.fail())
	{
		cout<<"The file was not successfully opened"<<endl;
		getch();
		exit(1);
	}

        else  
	{
		infile.get(c);
		infile.seekg(0, ios::end); 
		infile.seekg (0, ios::beg);

		string t;
		int lineCount=0;

		while(getline(infile, t, '\n'))
		{
			++lineCount; 
		}
		cout << "The number of lines in the file are " << lineCount << endl;
		
		std::string line ;
		while( std::getline( file, line ) )

		std::string line;
		while (getline(infile, line))
		{
			for(z = 0; z < count; z++) 
			{
				for(q=0; q < 27 ; q++)
				{
					if( descrip[z]== str2[q])				
					i[q]++;				
				}   
			}

			for(q=0; q < 26; q++)
			{
				double g;
  				g=i[q]/count;
				cout<<g<<endl;
			}
		}
		
	}
	infile.close();
	cout<<endl;

	getch();
	return 0;
}

Please use the code tags.

You can do everything within one loop, and you don't need any of the character arrays.

Are you limited to capital letters?

In pseudocode:

1. Read in a line using getline (with the std::string overload) increment the line count
2. Make an [B]int[/B] array that's 26 elements long
3. Crawl that line, checking to see if each character is greater than 'A' and less than 'Z' (or use isupper() in <cctype> if you are permitted).  Count each letter using your int array.  Use the count of characters that the std::string has in length()
4. Perform all the calculations necessary
5. Repeat ad nauseum

A minor point, but use if(!infile.is_open()) instead of fail() . Fail is to check if the stream goes bad.

infile.get(c);
    infile.seekg(0, ios::end); 
    infile.seekg (0, ios::beg);

You're kidding, right? For heaven's sake why?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.