4 Replies - 15535 Views - Last Post: 06 August 2008 - 03:43 AM Rate Topic: -----

#1 xiaolim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 28-July 08

reading every characters in a text file

Posted 05 August 2008 - 01:25 AM

hi, i written a programe that will open a file and then read all the characters in it and display them out. so here is my code:


#include <iostream> 
#include <cstring>  
#include <fstream>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <cstdlib>

using namespace std;


int main(void)
{

	ifstream fin;
	ofstream fout;
	string filename;
	
	cout << "Welcome\n" << endl;
	cout << "This programe will analyze the file content &" << endl;
	cout << "compute the statistics of the file you input.\n\n\n\n\n" << endl;
	system("pause");
	system("cls");

	
	do 
	   {
		cout << "Enter input data file name:\n";
		cin >> filename;
		cout << "\n";	// Get the file name.
		fin.open(filename.c_str());  // Convert to C-string and open it.
		if (!fin) 
			 {		  // Will fail if didn't exist.
				cout << "Unable to open " << filename << endl;
				cin.get();
				system("cls");
				
			 } 
		} while(!fin);
  
  
  
	   
	char next;
	int characters = 0;
	int digits = 0;
	int upper = 0;
	int lower = 0;
	int space = 0;
	int eospm = 0;
	int others = 0;
	

	while((next = fin.get()) != EOF)
	{
		  //calculate total numbers of characters including space	---> it doesnt count all the characters, what should i use to count every characters including space?
		  if(isalnum(next))
		  characters++;
		  
		  //calculate total numbers of digits   ---> correct
		  if(isdigit(next))
		  digits++;
		  
		  //calculate total numbers of uppercase   ---> correct
		  else if(isupper(next))
		  upper++;
		  
		  //calculate total numbers of lowercase   ---> correct
		  else if(islower(next))
		  lower++;
		  
		  //calculate total numbers of space	---> this 1 doesnt count the correct whitespace either.
		  else if(isspace(next))
		  space++;
		  
		  //calculate total numbers of punctuation   ---> correct
		  else if(ispunct(next))
		  others++;		 
	  
	}

	// Print out what is counted.
	cout << "Total Number of Characters: " << characters << endl; 
	cout << "" << endl;
	cout << "Total Number of Uppercase: " << upper << endl; 
	cout << "Total Number of Lowercase: " << lower << endl; 
	cout << "Total Number of Digits: " << digits << endl; 
	cout << "Total Number of Space: " << space << endl; 
	cout << "Total Number of End-of-Sentence Punctuation Marks: " << eospm << endl; 
	cout << "Total Number of Other Characters: " << others << endl;	  
	cout << "" << endl;

	   
	   system("pause");
	   
return 0;
}



alright, here is the problem, for "upper", "lower", "digits" and "other" all displayed correctly,but the others were wrong, i wanted to count all the characters in the file, End-of-Sentence Punctuation Marks( those punct in the end of a sentence ) and the number of white space in between the sentence. the all characters count have a wrong output as well as the whitespace, End-of-Sentence Punctuation Marks have no output. can anyone try on some text file and see where my code went wrong? any help is greatly appreciated. thanks

Is This A Good Question/Topic? 0
  • +

Replies To: reading every characters in a text file

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: reading every characters in a text file

Posted 05 August 2008 - 01:47 AM

To count all characters, you need to increment counter for every iteration but you are doing it only for alphanumerics and getting wrong result.

isspace counts whitespace, which means it counts ' ', '\n', '\r', '\t' etc. if you are expecting it to count space only then it might fail. Plus it might be possible that it is considering newline as "\n\r" this depends on file formats sometimes. Look at this possibility also.

see here if you need more info related to functions you used and which function counts what.

I hope this will help you. :)
Was This Post Helpful? 0
  • +
  • -

#3 jwwicks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 162
  • Joined: 31-July 08

Re: reading every characters in a text file

Posted 05 August 2008 - 02:34 AM

Hello xiaolim,

View Postxiaolim, on 5 Aug, 2008 - 01:25 AM, said:

hi, i written a programe that will open a file and then read all the characters in it and display them out. so here is my code:

alright, here is the problem, for "upper", "lower", "digits" and "other" all displayed correctly,but the others were wrong, i wanted to count all the characters in the file, End-of-Sentence Punctuation Marks( those punct in the end of a sentence ) and the number of white space in between the sentence. the all characters count have a wrong output as well as the whitespace, End-of-Sentence Punctuation Marks have no output. can anyone try on some text file and see where my code went wrong? any help is greatly appreciated. thanks


Just a couple problems with the code, Total characters is everything put together, isalnum just does the same thing as isdigit & lower upper.

Here's my fix...
#include <iostream> 
#include <fstream>
#include <string>
#include <cstdio>
#include <cstdlib>

using namespace std;
#ifdef _WIN32
    const char CLRSCR[] = {"cls"};
    const char PAUSE[] = {"pause"};
#else
    const char CLRSCR[] = {"clear"};
    const char PAUSE[] = {"sleep(20)"};
#endif

int main(void)
{

    ifstream fin;
    ofstream fout;
    string filename;
    
    cout << "Welcome\n" << endl;
    cout << "This program will analyze the file content &" << endl;
    cout << "compute the statistics of the file you input.\n\n\n\n\n" << endl;
    system(PAUSE);
    system(CLRSCR);

    
    do{
        cout << "Enter input data file name:\n";
        cin >> filename;
        cout << "\n";    // Get the file name.
        fin.open(filename.c_str());  // Convert to C-string and open it.
        if (!fin) 
             {          // Will fail if didn't exist.
                cout << "Unable to open " << filename << endl;
                cin.get();
                system(CLRSCR);
                
             } 
    }while(!fin);
  
      
    char next;
    int characters = 0;
    int digits = 0;
    int upper = 0;
    int lower = 0;
    int space = 0;
    int eospm = 0;
    int others = 0;
    

    while((next = fin.get()) != EOF)
    {
          //calculate total numbers of characters including space    
          //---> it doesnt count all the characters, what should i use to count every characters including space?
          //The function isalnum() returns non-zero if its argument 
          //is a numeric digit or a letter of the alphabet. Otherwise, zero is returned
          if(isalnum(next))
            characters++;
          
          //calculate total numbers of digits   ---> correct
          if(isdigit(next))
            ++digits;
          
          //calculate total numbers of uppercase   ---> correct
          if(isupper(next))
           ++upper;
          
          //calculate total numbers of lowercase   ---> correct
          if(islower(next))
           ++lower;
          
          //calculate total numbers of space    ---> this 1 doesnt count the correct whitespace either.
          if(isspace(next))
          ++space;
          
          //calculate total numbers of punctuation   ---> correct
          if(ispunct(next)){
              ++others;
              switch(next){
                  case '?':
                  case '.':
                  case '!':
                      ++eospm;
                      break;
                  default:
                      break;
              }
          }
      
    }

    // Print out what is counted.
    cout << "Total Number of Characters: " << space+digits+upper+lower+others << endl; 
    cout << "" << endl;
    cout << "Total Number of Uppercase: " << upper << endl; 
    cout << "Total Number of Lowercase: " << lower << endl; 
    cout << "Total Number of Digits: " << digits << endl; 
    cout << "Total Number of Space: " << space << endl; 
    cout << "Total Number of End-of-Sentence Punctuation Marks: " << eospm << endl; 
    cout << "Total Number of Other Characters: " << others << endl;      
    cout << "" << endl;

       
       system(PAUSE);
       
    return 0;
}



Here's the file I used...
1 2 3 4 5 6 7 8 9 10
; ' " : ? ! . ,
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
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
sentence.
SENTENCE.



digits = 11 Because you're counting individual characters. If you want to count 10 as one digit then you'll need to track what the previous character was and if the previous character and the current character are digits then don't count it.

total = 160
upper = 34
lower = 34
spaces = 71
eospm = 5
others = 10

JW
Was This Post Helpful? 0
  • +
  • -

#4 xiaolim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 28-July 08

Re: reading every characters in a text file

Posted 06 August 2008 - 03:35 AM

wao.. it worked, thanks alot, you save my life
Was This Post Helpful? 0
  • +
  • -

#5 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: reading every characters in a text file

Posted 06 August 2008 - 03:43 AM

To add to that, jwwicks used the system() function quite a bit.

Try checking my snippets for "redefining clrscr()" for a function to clear the screen, and replace "system("cls");" with a call to "clrscr()"

To pause for input, rather than using "system("pause");" you could use cin.get().

Here's an article as to why not to use "system()" whenever possible:
http://www.gidnetwork.com/b-61.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1