Thread: Read the number of characters

  1. #1
    Registered User
    Join Date
    Jun 2007
    Location
    Qatar
    Posts
    39

    Read the number of characters

    Hi,
    I want to count the number of characters in a text file and want to sore them in an array.(Remember its a file not a string).Thanxx

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Okay,

    * Open the file
    * Move the pointer to the end of the file, and get it's position (ie fseek() and ftell()) Which will tell you it's size in bytes
    * Allocate a buffer on the heap (ie, dynamically allocated), big enough to hold the file
    * Read the file into the buffer
    * Close the file

    * Post your problems here, at least have a crack at it! (Sounds like homework to me!)

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    12
    Something I wrapped up that should work..

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    const std::string file = "filename.txt";
    
    // string that is going to contain the file's contents
    std::string str = "";
    
    int main(void) {
        // construct a datastream named fin to the specified file
        std::ifstream fin(file.c_str(), std::ios_base::in);
    
        // return false if the function couldn't write to file
        if (!fin.is_open()) 
            fin.clear();
            
        // temp variable used to contain each char
        char ch; 
        
        // variable that counts each character in the file
        int count = 0;   
            
        // loop trough each character in the file
        while (fin.get(ch)) {
            count++;
            str+=ch;
        }
        
        // show the amount of characters in the file
        std::cout << count;
        
        // show the contents which is stored in the std::string 'str'
        std::cout << std::endl << str;
            
        // close file output stream
        fin.close();
    
        return 0;
    }

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by rehan View Post
    Hi,
    I want to count the number of characters in a text file and want to sore them in an array.(Remember its a file not a string).Thanxx
    How about a vector instead?

    Code:
    #include <algorithm>
    #include <iterator>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include <vector>
    
    std::vector<char> load_file(const std::string &name)
    {
       std::vector<char> output;
       std::ifstream input(name.c_str());
       input >> std::noskipws;
       std::copy(std::istream_iterator<char>(input),
                 std::istream_iterator<char>(), std::back_inserter(output));
       return output;
    }
    This loads a file into a vector of char. The length of the file can be found by calling this vector's size() method.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Location
    Qatar
    Posts
    39
    Thanxx buddies,
    Now i got it ,but i do not want to keep the spaces in my array.I mean i want to ignore the spaces and store the next character in the array instead of space.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    store the next character in a temporary char and check if its a space
    if its not, add it to the array

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number Guessing
    By blacknapalm in forum C Programming
    Replies: 2
    Last Post: 10-01-2008, 01:48 AM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  3. I am lost on how to read from file and output to file?
    By vicvic2477 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 11:52 AM
  4. How to read single characters in a string
    By MaxxMan-X in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2005, 02:52 PM
  5. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM