Thread: Need help counting the number of characters in a .txt file

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    1

    Question Need help counting the number of characters in a .txt file

    Hello, I have recently hit a stump with C++ and have been getting pretty frustrated with this assignment. I just can't seem to find out how to start the assignment. Basically for the first part of the assignment, I need to find the number of characters in this .txt file my teacher provided for me. The only exception is that I can only count the actual letters themselves, not spaces or punctuation. How would I go about doing this? I have read about functions like isalpha but can't figure out on how to fit them into code to do the first part of this assignment. Could someone give me some kind of an example to get headed in the right direction on this?

    Thanks for any help,
    Torm04

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    We are all here to help, but you are going to have to produce some code. Please refer to the:Announcements - General Programming Boards.

    So, why don't you show us what you have so far? Like opening the file, starting to read it? Here is the C Boards very own: C++ File I/O Tutorial - Cprogramming.com.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Okay, because I am in a good mood:

    Code:
    #include <iostream>
    #include <fstream>
    #include <locale>
    #include <algorithm>
    
    
    void MakeTestFile(void)
    {
    	std::fstream testFile("example.txt", std::ios::out);
    	if (testFile.is_open())
    	{
    		testFile << "This is &%A line of text1243 !*&gF23";
    	}
    	testFile.close();
    }
    int main(void)
    {
    	MakeTestFile();
    
    	std::fstream testFile("example.txt", std::ios::in);
    
    	if (testFile.is_open())
    	{
    		std::locale loc;
    
    		auto charCount = std::count_if(std::istream_iterator<char>(testFile),
                                                   std::istream_iterator<char>(),
                                                   [=](char c){return std::isalpha(c, loc); });
    
    		std::cout << "Number of characters: " << charCount;
    
    		
    	}
    	testFile.close();
    
    	std::cin.get();
    	return 0;
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble counting number of characters copied to file
    By Tripswitch in forum C Programming
    Replies: 13
    Last Post: 07-22-2014, 08:15 AM
  2. Replies: 2
    Last Post: 12-17-2013, 11:50 AM
  3. Counting characters in file using fscanf
    By jacobsh47 in forum C Programming
    Replies: 9
    Last Post: 04-07-2011, 12:23 PM
  4. Counting the characters from each word from a text file
    By flipguy_ph in forum C Programming
    Replies: 6
    Last Post: 04-27-2009, 05:56 PM
  5. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM

Tags for this Thread