Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,059 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,536 people online right now. Registration is fast and FREE... Join Now!




reading every characters in a text file

 
Reply to this topicStart new topic

reading every characters in a text file

xiaolim
5 Aug, 2008 - 12:25 AM
Post #1

New D.I.C Head
*

Joined: 28 Jul, 2008
Posts: 10

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:

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
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Reading Every Characters In A Text File
5 Aug, 2008 - 12:47 AM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 times
Dream Kudos: 125
My Contributions
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. smile.gif
User is offlineProfile CardPM
+Quote Post

jwwicks
RE: Reading Every Characters In A Text File
5 Aug, 2008 - 01:34 AM
Post #3

D.I.C Head
Group Icon

Joined: 31 Jul, 2008
Posts: 59



Thanked: 6 times
Dream Kudos: 200
My Contributions
Hello xiaolim,

QUOTE(xiaolim @ 5 Aug, 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:

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...
cpp

#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...
CODE

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
User is offlineProfile CardPM
+Quote Post

xiaolim
RE: Reading Every Characters In A Text File
6 Aug, 2008 - 02:35 AM
Post #4

New D.I.C Head
*

Joined: 28 Jul, 2008
Posts: 10

wao.. it worked, thanks alot, you save my life
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Reading Every Characters In A Text File
6 Aug, 2008 - 02:43 AM
Post #5

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,522



Thanked: 96 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
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
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 06:19PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month