Program to Count Character Occurrences in a File

  • Thread starter Thread starter neo programmer
  • Start date Start date
  • Tags Tags
    File Program
Click For Summary

Discussion Overview

The discussion revolves around a program intended to count the occurrences of each character in a specified file, with a focus on C++ programming practices. Participants are providing feedback on the code structure, logic, and potential improvements, as well as addressing specific programming issues related to file handling and character counting.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses a need for help with their C++ program that counts character occurrences, noting that their current implementation is incorrect.
  • Another participant questions the use of arbitrary numbers in the code, such as the size of the filename array and the character array, suggesting that these should not be "magic numbers."
  • Concerns are raised about the use of the eof() function in the while loop, with a suggestion to use file_ptr.get(c) for reading characters instead.
  • A participant mentions the importance of adding comments to the code for clarity and suggests simplifying the code structure.
  • One participant explains their reasoning for using ASCII values in their character counting approach, specifically noting the range for uppercase and lowercase letters.
  • Another participant provides a more appropriate method for reading characters from a file, emphasizing the need to avoid reading past the end of the file.

Areas of Agreement / Disagreement

Participants express differing views on coding practices, particularly regarding the use of magic numbers and file reading methods. There is no consensus on the best approach to implement the character counting functionality, and multiple suggestions are presented.

Contextual Notes

Limitations include unresolved questions about the filename handling and the potential for input errors when reading from files. The discussion highlights various assumptions about character encoding and file structure that are not explicitly addressed.

neo programmer
Messages
4
Reaction score
0
hi! i was writing a program which asks the user to input the file name. the program then reads the file and prints out how many times each characeter has occurred in the file(uppercase and lowercase are different). Pls help me out.
this is what i have got-i know this is not correct.

#include<iostream>
#include<fstream>
#include<stdlib.h>

using namespace std;

int main()
{
fstream file_ptr;
char filename[81];
int a = 65;
int e=0;
char number[58]={0};
for(int i=0; i<58; i++)
{
number=a;
a++;
}
// Get name of file
count << "Enter filename : ";
cin.getline(filename,81);
// Open data file for READING
count << "Opening file "<< filename << endl;
file_ptr.open(filename, ios::in);
if (!file_ptr.good())
{
//The file could not be opened
count << "ERROR - file " << filename << " could not be opened";
exit(1);
}
// READ data from file until the end-of-file (EOF) is reached.

do {
file_ptr >> number;
count << "Read " << number << " from file." << endl;
} while(file_ptr.peek()!=EOF);
// Close file
count << "Closing file " << filename << endl;
file_ptr.close();
system("pause");
return 0;
}
 
Technology news on Phys.org
Code:
char filename[81];
Why 81? What's 81 char big? Don't use http://www.parashift.com/c++-faq-lite/newbie.html".

Code:
int a = 65;
Why 65? What's 65? Don't use magic number. You don't need to tell the program what it's internal representation is in numerically for characters. Use char's and it'll work it out itself.

Code:
int e=0;
What's e? Where have you used it?

Code:
char number[58]={0};
Why 58? What's 58? Don't use magic number.

Code:
for(int i=0; i<58; i++)
{
number[i]=a;
a++;
}
Don't need that.

Code:
cin.getline(filename,81);
Use std::string, and not some random arbitrary length for the filename.

Code:
do {
file_ptr >> number;
cout << "Read " << number << " from file." << endl;
} while(file_ptr.peek()!=EOF);
>> reads in from the first non-whitespace character until the next non-whitespace character. number confusing though you've made it, is char array of 58 char's long. You're wanting to read in each character one at a time, and count it. Why are you reading in phase at a time?
 
Last edited by a moderator:
hmm, man, I hate C++. C++ is way too overloaded (it's a joke). Try placing comments or exlanations on each part of your code, so you, yourself, and others know what a section does. Look at all those questions by KTC. Try simplifying your code, or remove code that isn't really needed. And I believe, C++ has helper functions for getting on a character by character basis. Anyway, I'll check again and post here another time.
 
this is something different which i wrote

#include<iostream>
#include<fstream>
#include<stdlib.h>

using namespace std;

int main()
{
fstream file_ptr;
char filename[81];
// Get name of file
count << "Enter the filename (make sure that the test file and the program is in the same directory): ";
cin.getline(filename,81);
// Open data file for READING
count << "Opening file "<< filename << endl;
file_ptr.open(filename, ios::in);
if (!file_ptr.good())
{
//The file could not be opened
count << "ERROR - file " << filename << " could not be opened";
exit(1);
}
int a = 65;
char number[58]={0};
for(int i=0; i<58; i++)
{
number=a;
a++;
}
int x[58]={0};
char c;
// READ data from file until the end-of-file (EOF) is reached.
while(file_ptr.eof()==0)
{
file_ptr.get(c); //reading character
for(int i=0; i<58; i++)
{
if(number==c)
{
x=x+1;
break;
}
}
}
for(int i=0; i<58; i++)
{
count<<number<<"="<<x<<endl;
}

// Close file
count << "Closing file " << filename << endl;
file_ptr.close();
system("pause");
return 0;
}
 
i took 65 because 65 is ASCII equivalent of 'a'. as i wnt to get all the alphabets(both uppercase and lowercase) so i used size of d array as 58.
65 + 58 =123 which is ASCII equivalent of 'Z'.

i still don't know how to correct the filename part in d code.
 
When reading files using a "while" loop in C++, it is almost never a good idea to use the eof() member function or the predefined EOF symbol, because it gives you "true" only after the program has tried to read past the end of file and failed. Therefore the loop makes one more cycle after the last real data item, giving you some garbage input.

In your case, the usual way to read one character at a time is:

Code:
while (file_ptr.get(c))
{
    // do your stuff with c here
}

When you use any of the standard C++ input operations in a boolean context, it evaluates as "true" or "false" depending on whether the operation succeeded or not, in addition to actually performing the input operation. Therefore, when file_ptr.get() tries to read past the last character in the file, it fails, and the loop terminates immediately without trying to do anything with the garbage that is now in c.

The appropriate way to use eof() is to test whether the loop ended because of end of file or some other kind of input error:

Code:
while (file_ptr.get(c))
{
    // do your stuff with c here
}

if (file_ptr.eof())
    cout << "End of file reached normally." << endl;
else
    cout << "There was an input error." << endl;
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 33 ·
2
Replies
33
Views
3K
Replies
10
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
5K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 65 ·
3
Replies
65
Views
8K
  • · Replies 66 ·
3
Replies
66
Views
6K