Thread: Wordlist problem with code!!

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    Question Wordlist problem with code!!

    Obviously fro the source you can see i have no idea what i'm doing, but i hope you get the idea.

    Code:
    #include <fstream.h>
    #include <iostream.h>
    
    int main()
    {
        ifstream infile("input.txt");
        ofstream outfile("wordlist.txt");
    
        char word;
        
        while (infile >> word)
          {
    	 if (word == ' ') word = '\n';
    	 outfile << word;
    	 infile.get(word);
    	 outfile.put(word);
          }
    
       return 0;
    }
    The input.txt looks like this
    Code:
    ad am an as at ax cat dog pig horse animal
    I'm trying to make the out put file look like this..
    Code:
    ad
    am 
    an 
    as 
    at 
    ax 
    cat 
    dog 
    pig 
    horse 
    animal
    "you get the idea...."
    How do i accomplish this????
    Last edited by denwar; 04-10-2004 at 09:47 PM.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Code:
    while (infile >> word)  // you input a character
    {
        if (word == ' ') word = '\n';
        outfile << word;    // you output the character
        infile.get(word);   // you now attempt to input multiple 
                            //    characters into a single char
        outfile.put(word);  // get rid of these two lines
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    Code:
    #include <fstream.h>
    #include <iostream.h>
    
    int main()
    {
        ifstream infile("input.txt");
        ofstream outfile("wordlist.txt");
    
        char word;
        
        while (infile >> word)
          {
    	 if (word == '@') word = '\n';
    	 outfile << word;
    	// infile.get(word);
    	// outfile.put(word);
          }
    
       return 0;
    }
    I've noticed that if i use
    Code:
      if (word == ' ') word = '\n';
    my wordlist comes out incorrect. However if i replace the whitespace within the original "input.txt" to '@' & use the following code
    Code:
      if (word == '@') word = '\n';
    the output is correct.

    Anyone know why did is happening?

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    32
    This will do what you want.

    Code:
    int main()
    {
    	 ifstream infile("input.txt");
    	 ofstream outfile("wordlist.txt");
    
    	 char *word; //can hold a string of any length
    
    	 while (!infile.eof())
    	{
    	   word = new char;
      	   infile >> word >> ws;
    	   outfile << word << '\n';
    	}
    
             infile.close();
    	 outfile.close();
    	 return 0;
    }
    Jules

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you cannot put whitespace characters like space, tab, newline, etc. into variables using the >> operator because the >> operator ignores leading whitespace and terminates input into variables when the first non-leading whitespace char is encountered. In order to recognize white space characters so you can analyze them you can use the getline() method. Since @ isn't a whitespace char it is stored by >> and can be parsed on.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >while (!infile.eof())
    The eof member function is not meant to be used as a loop condition. This is wrong.

    >word = new char;
    Why use dynamic allocation for a single char?

    >infile.close();
    >outfile.close();
    These are closed automatically in the destructor.
    Code:
    #include <cstdlib>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      ifstream in ( "input.txt" );
      ofstream out ( "wordlist.txt" );
    
      if ( !in.is_open() || !out.is_open() )
        return EXIT_FAILURE;
      string word;
      while ( in>> word )
        out<< word <<'\n';
    }
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    hi prelude, i am also watching this topic. ...your solution is compact and straightforward.

    one small question on your code. it is a very silly question i agree .........why you are writing return EXIT_FAILURE;
    is there anything like EXIT_SUCCESS ? ? the naming is inappropriate here. Bcoz if none of the condition satisfied then really it will exit ... so it should have been EXIT_SUCCESS.(if it exist at all)

    however, dont you think the naming is inappropriate? of course i understand i have to obey the grammer of the language.
    blue_gene

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >why you are writing return EXIT_FAILURE
    Because if one of the files fails to open, the program should terminate, preferrably with an error.

    >is there anything like EXIT_SUCCESS ?
    Yes, though I didn't use it here because the appropriate value is returned automagically when the closing brace of main is reached.

    >dont you think the naming is inappropriate?
    No. Using EXIT_SUCCESS for unsuccessful termination doesn't make sense, so I used EXIT_FAILURE because if one of the files fails to open, the program can't do what it was written to do.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    "....EXIT_FAILUREbecause if one of the files fails to open..."

    hmmm...ok. it was a misunderstanding of the phrase with the SUCCESS.


    however, i used to write exit(0) if it fails.

    thanks for the clarification
    blue_gene

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >however, i used to write exit(0) if it fails.
    That's the same thing as return 0.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    or as return EXIT_SUCCESS.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM