Thread: Reading a File and returning single words

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Germany
    Posts
    12

    Reading a File and returning single words

    Hello everybody,

    My goal with this programm is to implement a function that reads a file with text, and returns successively single words, that can be handled by another function.

    One of the problems is, that for some reason it stops giving outputs at the last word and stays running instead of breaking up ( return 0; ).

    I wonder also, how could/should I implement this as a method, that throws all the words of the file, so that they can be handled by another method?

    Thanks in advance!

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    
    int main()
    {
    	char ch;
    	std::string word = "";
    	
    	// Givin the InputFile a more suitable name.
    	std::fstream inputFile("test.txt");
    	
    	// pointer initialisation, pointing to first character
    	FILE *fptr = fopen("test.txt", "r");
    	
    	// ch has the first character of the file
    	ch = getc( fptr );
    	
    	// Read till end of file (problem: it doesnt stop after printing the last word) 
    	while(!inputFile.eof())
    	{		
    		if ( (ch != ' ')  && (ch != '\n')  )
    		{
    			word += ch;
    			ch = getc( fptr );
    			
    			// Reading ' ' or '\n' implies the end of a word. print it.
    			if( (ch == ' ') || (ch == '\n') ) {
    				std::cout << word << std::endl;
    				word = "";
    			}
    		}
    		if( (ch == ' ') || (ch == '\n')) { 
    			ch = getc( fptr );
    		}
    		if( !((ch != ' ') || (ch == ' ')) ) {
    			ch = getc( fptr );
    		}
    	}
    	return 0;
    }
    Last edited by kapri; 05-10-2005 at 07:59 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    Try this:

    Code:
    // Komplett funktionierende Version, nur kuerzer.
    
    #include <string>
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main() {
      string s;
      ifstream f("word.cc");
    
      do {
       f >> s;
       cout << s << endl;  // pass s to a function or do whatever you want
      } while (!f.eof());
    }
    Oh, I see that my program fails if the file does not exist. You need to check
    for this, of course.

    Code:
    if (!f)
      // could not open file
    Last edited by rpet; 05-10-2005 at 08:02 PM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    One of the problems is, that for some reason it stops giving outputs at the last word and stays running instead of breaking up ( return 0; ).
    You need to make your read statement the loop condition. That way if anything goes wrong with the read attempt, the loop will terminate:
    Code:
    while(ch = getc( fptr ))
    {
    
    }
    I wonder also, how could/should I implement this as a method, that throws all the words of the file, so that they can be handled by another method?
    1) You could create an array of cstrings in main() and send the array to your function, which can read from the file and fill up the array.

    2) You could create an array of C++ strings in main(), then send the array to the function, which can read from the file and fill up the array.

    3) You can create a <vector> in main(), then send the <vector> to the function, which can read from the file and fill up the <vector>. An advantage to using a vector is that it can be resized to accommodate how many words you read. Instead of having to declare an array of size, say, 1000 to accommodate the largest possible number of words you may read in, you can keep resizing a <vector> to accommodate the actual number of words you read in.
    Last edited by 7stud; 05-10-2005 at 09:21 PM.

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Germany
    Posts
    12
    Quote Originally Posted by 7stud
    You need to make your read statement the loop condition. That way if anything goes wrong with the read attempt, the loop will terminate:
    Code:
    while(ch = getc( fptr ))
    {
    
    }
    I tried doing it so, but for :

    Code:
    while(ch = getc( fptr )) // !inputFile.eof();
    {
                    std::cout << ch;
    }
    it doesnt stop by the end of the file. After reading the last word in the file it starts to give an "y" character with two points over it as output and doesnt stop.

    Thanks for the replies! I'll keep trying, if i get rid of it, I'll post the solution.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    Sorry, getc() returns an int, so that won't work.

    Is there any reason you are reading in a char at a time? The extraction operator >> is defined to read in words. It seems to me your whole while loop can be replaced by:


    Code:
    ifstream inFile("C:\\TestData\\input.txt");
    if(!inFile)
    {
    	cout<<"bad file name"<<endl;
    	return 1;
    }
    
    string input[100];
    int count = 0;
    
    while(inFile>>input[count++])
    {		
    }
    You need to include <fstream> and <string> for that to work.
    Last edited by 7stud; 05-11-2005 at 02:44 AM.

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Germany
    Posts
    12
    Quote Originally Posted by 7stud
    Is there any reason you are reading in a char at a time?
    Argg, no there wasn't a reason to do so. I just didn't realize that >> reads full words
    With that it runs wonderfull

    Thank you really much for the help! I was stuck there for a while.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-05-2009, 09:42 AM
  2. fopen returning "Bad File Pointer"
    By jprukop in forum C Programming
    Replies: 4
    Last Post: 04-17-2009, 03:01 PM
  3. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. reading in array and returning length
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2002, 09:25 PM