Thread: Need help on string read

  1. #1
    Unregistered
    Guest

    Need help on string read

    Just say i want to read a amount of words, lines, and characters in a .dat file. Im thinking my string algoritim would look something like this:

    ------------------------------------------
    //for words, lines, characters count read file, and then out_stream or cout results
    //in_stream is already connected to words.dat

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstring>

    int words, lines, characters;
    ifstream in_stream;
    ofstream out_stream;

    while(!in_stream.eof())
    {

    //chracter readout, and cout result
    characters = strlen("words.dat");
    out_stream << "words.dat has " << characters << " characters.";

    //lines readout, and cout result

    //words readout, and cout result

    }

    I cant think of an algoritum for lines, and words. I do have idea of lines, i think its something like this "\n" , line++ . I think words is something like this "\0" word++ . The thing is, i can't translate it into code form, do I use strlen(), or getline()??? Please help, or suggestion...Thank You!

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    read content of file in one char at a time. Increment char counter. Evaluate each char before reading the next. If char is (== operator) newline char ('\n') increment appropriate counter. Else if char is space (' ') (isspace(char)) increment appropriate word counter.

    When all done you may have overcounted words since there are usually two spaces between sentences, but if file isn't text in paragraph form then number of spaces is good first estimate to number of words.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Replies: 1
    Last Post: 05-30-2003, 02:31 AM
  4. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM
  5. read records fron file into a binary tree
    By Kirsten in forum C Programming
    Replies: 1
    Last Post: 04-23-2002, 02:48 PM