Text file reading and separating words

Discussion in 'C++' started by Pali, Nov 3, 2010.

  1. Pali

    Pali Guest

    I need to read a line from a text file by character by character. Then
    assign the words in that line, which are separated by spaces in to
    separate strings. Pl. tell me how to code this from c++.
     
    Pali, Nov 3, 2010
    #1
    1. Advertising

  2. Pali

    muler Guest

    On Nov 3, 2:11 pm, Pali <> wrote:
    > I need to read a line from a text file by character by character. Then
    > assign the words in that line, which are separated by spaces in to
    > separate strings. Pl. tell me how to code this from c++.


    hi, the following program reads a file, m.cpp, a word at a time,
    pushes that into a container (such as vector), sorts the container,
    and finally prints each word.

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    #include <algorithm>

    int main(int argc, char* argv[])
    {
    using namespace std;
    ifstream in("m.cpp");
    if(!in) {
    cerr << "can't open file!";
    return -1;
    }
    // file ok
    vector<string> vec;
    string word;
    while(in >> word)
    vec.push_back(word);

    in.close();

    // sort words
    sort(vec.begin(), vec.end());

    // print
    typedef vector<string>::const_iterator VCI;
    for(VCI i = vec.begin(); i != vec.end(); ++i)
    cout << *i << endl;

    return 0;
    }
     
    muler, Nov 3, 2010
    #2
    1. Advertising

  3. On Nov 3, 2:11 pm, Pali <> wrote:
    > I need to read a line from a text file by character by character. Then
    > assign the words in that line, which are separated by spaces in to
    > separate strings. Pl. tell me how to code this from c++.


    It seems it's a homework and we don't do them.
    Just some clues:
    1. Use C++ I/O stream, in particular file stream
    and getline() function
    2. Use string stream to extract words.
    If you access to The C++ Programming Language, 3rd edition by Bjarne
    Stroustrup
    read chapter 21.
    Of course there are other ways to solve the problem.

    Good luck
    -- Saeed Amrollahi
     
    Saeed Amrollahi, Nov 3, 2010
    #3
  4. Pali

    osmium Guest

    Saeed Amrollahi wrote:

    > On Nov 3, 2:11 pm, Pali <> wrote:
    >> I need to read a line from a text file by character by character.
    >> Then assign the words in that line, which are separated by spaces in
    >> to separate strings. Pl. tell me how to code this from c++.

    >
    > It seems it's a homework and we don't do them.
    > Just some clues:
    > 1. Use C++ I/O stream, in particular file stream
    > and getline() function
    > 2. Use string stream to extract words.
    > If you access to The C++ Programming Language, 3rd edition by Bjarne
    > Stroustrup
    > read chapter 21.
    > Of course there are other ways to solve the problem.


    Step 0. Define a word.
     
    osmium, Nov 3, 2010
    #4
  5. Pali

    James Kanze Guest

    On Nov 3, 11:39 am, Saeed Amrollahi <> wrote:
    > On Nov 3, 2:11 pm, Pali <> wrote:


    > > I need to read a line from a text file by character by
    > > character. Then assign the words in that line, which are
    > > separated by spaces in to separate strings. Pl. tell me how
    > > to code this from c++.


    > It seems it's a homework and we don't do them.


    It's clearly homework, since otherwise, there would be no
    requirement to read character by character, and you'd just use
    << into a string (which does exactly what he wants).

    --
    James Kanze
     
    James Kanze, Nov 3, 2010
    #5
  6. Pali

    Helge Kruse Guest

    "osmium" <> wrote in message
    news:...
    >> On Nov 3, 2:11 pm, Pali <> wrote:
    >>> Then assign the words in that line, which are separated by spaces in


    > Step 0. Define a word.

    Hasn't that been done?


    Helge
     
    Helge Kruse, Nov 4, 2010
    #6
  7. Pali

    osmium Guest

    "Helge Kruse" wrote:

    > "osmium" <> wrote in message
    > news:...
    >>> On Nov 3, 2:11 pm, Pali <> wrote:
    >>>> Then assign the words in that line, which are separated by spaces in

    >
    >> Step 0. Define a word.

    > Hasn't that been done?


    It's been done by a great many people in a great many ways - that's the
    problem. What is the status of the hyphen? Is humpty-dumpty a word? Or is
    it two words? Seven is obviously a word, what about 7? The OP has to come
    up with a definition which will satisfy his instructor.
     
    osmium, Nov 4, 2010
    #7
  8. Pali

    Pali Guest

    On Nov 3, 7:11 pm, Pali <> wrote:
    > I need to read a line from a text file by character by character. Then
    > assign the words in that line, which are separated by spaces in to
    > separate strings. Pl. tell me how to code this from c++.


    Thank you for all replied to me.
    I am an old type programmer used Turbo Pascal and C 10 years back. Now
    I need a simple word extracting program. I thought reading char by
    char is easy, but it is OK, if I can read the word directly. So I
    tried as follows at the first time and it is not working.

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int main()
    {
    FILE *fp; /* file pointer */
    char ch;
    char s1[80];

    strcpy(s1,"");

    /* open file for input */
    if ((fp = fopen("Example.txt", "r"))==NULL) {
    printf("Cannot open file \n");
    exit(1);
    }

    /* look for character */

    while ((ch = getc(fp)) !=EOF )
    {
    while (ch != " ")
    { strcat (s1, ch);
    cout << ch;
    cout << s1;
    }
    }
    fclose(fp);
    cout << s1;
    cin >> ch; /* to check the o/p */
    }
     
    Pali, Nov 5, 2010
    #8
  9. Pali

    Ian Collins Guest

    On 11/ 5/10 03:19 PM, Pali wrote:
    > On Nov 3, 7:11 pm, Pali<> wrote:
    >> I need to read a line from a text file by character by character. Then
    >> assign the words in that line, which are separated by spaces in to
    >> separate strings. Pl. tell me how to code this from c++.

    >
    > Thank you for all replied to me.
    > I am an old type programmer used Turbo Pascal and C 10 years back. Now
    > I need a simple word extracting program. I thought reading char by
    > char is easy, but it is OK, if I can read the word directly. So I
    > tried as follows at the first time and it is not working.


    What was wrong with the idiomatic solution using iostreams and
    std::string? It's much clearer and less error prone.

    --
    Ian Collins
     
    Ian Collins, Nov 5, 2010
    #9
  10. Pali

    red floyd Guest

    On 11/4/2010 7:25 PM, Ian Collins wrote:
    >
    > What was wrong with the idiomatic solution using iostreams and
    > std::string? It's much clearer and less error prone.
    >


    OP's instructor wouldn't accept it as meeting the assignment?
     
    red floyd, Nov 5, 2010
    #10
  11. Pali

    Default User Guest

    "Pali" <> wrote in message
    news:...

    > char ch;


    > while (ch != " ")
    > { strcat (s1, ch);


    This should have produced a diagnostic, as strcat() requires two char
    pointers. You are passing a pointer and a single char.

    I'm not sure why you are using C-style string-handling when you've been
    given hints on how to do it in C++. Regardless, if you are going to use the
    C standard library functions, look them up before use and ensure that they
    are used correctly. If you get any warnings, resolve those first. Don't just
    ignore them.




    Brian
    --
    Day 642 of the "no grouchy usenet posts" project.
    Current music playing: None.
     
    Default User, Nov 8, 2010
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jazzy
    Replies:
    3
    Views:
    10,651
    Gordon Beaton
    May 25, 2004
  2. Jijai
    Replies:
    3
    Views:
    437
    Peter Flynn
    Sep 22, 2004
  3. sahm
    Replies:
    4
    Views:
    42,708
    rel0aded911
    Nov 23, 2009
  4. tshad
    Replies:
    2
    Views:
    738
    tshad
    Jun 19, 2009
  5. Art Merkel
    Replies:
    4
    Views:
    145
    Adam Funk
    Feb 6, 2007
Loading...

Share This Page