Thread: Trouble with eof()

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41

    Trouble with eof()

    I am writing a program that reads in a file and converts the data to a different format. I am having issues with using the end of file function. The program compiles with out any error but when I run it I think it gets stuck in an infinite loop. Can anyone tell me what I am doing wrong? I am just learning C++ so any and all advice is appreciated!

    Code:
    /*
     *  RasMolMaker
     *  
     *
     *  Created by  on 6/22/10.
     *  Copyright 2010 __MyCompanyName__. All rights reserved.
     *
     */
    #include <fstream>
    #include <iostream>
    #include <string>
    
    int main(const int argc, const char** argv)
    {
    	char ch;
    	const char* c_str();
    	std::string str,suffix,RasFile;
    	std::string filename;
    	suffix = ".pdb";
    	
    	// Get filename and open stream
    	std::cout << "Enter filename:\n";
    	std::cin >> filename;
    	std::cout << "Opening " << filename << "...\n";	
    	std::ifstream infile(filename.c_str());
    	
    	// Create RasMol file and open stream
    	RasFile = filename + suffix;
    	std::cout << "Output will be to file " << RasFile << "\n";
    	std::ofstream outfile(RasFile.c_str());
    	
    	int index = 1;
    	double x,y,z;
    	while (!infile.eof())
    	{
    		//infile.get(ch, 1);
    		if (ch == 'm') 
    		{
    			//infile.getline(str, '\n');
    			continue;
    		}
    		else {
    			infile >> x >> y >> z;
    			outfile << "ATOM  " << index;
    			index ++;
    		}
    
    	}
    	return 0;
    }
    Edit: Also, when I uncomment the infile.get and infile.getline I get a ton of error messages. The first one is:

    Code:
    RasMolMaker.cpp:35: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::get(char&, int)’
    /usr/include/c++/4.2.1/istream:292: note: candidates are: typename std::basic_istream<_CharT, _Traits>::int_type std::basic_istream<_CharT, _Traits>::get() 
    [with _CharT = char, _Traits = std::char_traits<char>]
    Last edited by waterborne; 06-23-2010 at 11:36 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    First, the get member function come in a variety of flavors:
    int_type get( );
    basic_istream& get(
    char_type& _Ch
    );
    basic_istream& get(char_type *_Str,
    streamsize _Count);
    basic_istream& get(
    char_type *_Str,
    streamsize _Count,
    char_type _Delim
    );
    basic_istream& get(
    basic_streambuf<Elem, Tr> *_Strbuf
    );
    basic_istream& get(
    basic_streambuf<Elem, Tr> *_Strbuf,
    char_type _Delim
    );
    None of these match what you've got. When I compile I get an error because of this:
    1>------ Build started: Project: CProg4, Configuration: Debug Win32 ------
    1>Compiling...
    1>CProg4.cpp
    1>c:\documents and settings\******\my documents\visual studio 2005\projects\cprog4\cprog4.cpp(28) : error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::get(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'char' to 'char *'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    1>Build log was saved at "file://c:\Documents and Settings\******\My Documents\Visual Studio 2005\Projects\CProg4\Debug\BuildLog.htm"
    1>CProg4 - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Second, you should avoid using eof tests to control your loops. Unless you know what you're doing it usually ends up badly. You should instead test the result of the input operation, for example:
    Code:
    while(infile.get(ch))
    {
        // Do something
    }
    Edit: Also, when I uncomment the infile.get and infile.getline I get a ton of error messages. The first one is:
    See, now you've just changed things, you're original post said it compiled without errors but that older version had the infile.get() line uncommented which could not have been. Now you're getting the error I get and that's because you're using the wrong version of get.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41
    Thanks for the input! I looked up get() and getline() and made some changes. Now the code compiles and runs but ends after incorrectly reading 1 line. The new code is:

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    int main(const int argc, const char** argv)
    {
    	char ch,holder[100];
    	const char* c_str();
    	std::string str,suffix,RasFile;
    	std::string filename;
    	suffix = ".pdb";
    	
    	// Get filename and open stream
    	std::cout << "Enter filename:\n";
    	std::cin >> filename;
    	std::cout << "Opening " << filename << "...\n";	
    	std::ifstream infile(filename.c_str());
    	
    	// Create RasMol file and open stream
    	RasFile = filename + suffix;
    	std::cout << "Output will be to file " << RasFile << "\n";
    	std::ofstream outfile(RasFile.c_str());
    	
    	int index = 1;
    	double x,y,z;
    	while (infile.get(ch))
    	{
    		infile.get(ch);
    		if (ch == 'm') 
    		{
    			std::cout << "Inside if statement\n";
    			infile.getline(holder, '\n');
    			continue;
    		}
    		else {
    			std::cout << "Inside else statement\n";
    			infile >> x >> y >> z;
    			outfile << "ATOM  " << index;
    			outfile << "\t\t" << x << " " << y << " " << z << "\n";
    			index ++;
    		}
    
    	}
    	return 0;
    }
    The input file is

    Code:
     BOUNDARY
        2 
        3 orthorhombic       2.9955490000e+01    2.9955490000e+01    2.9955490000e+01
        4 
        5 MOLECULES
        6 
        7 species    0
        8 nMolecule  588
        9 
       10 molecule  0
       11     1.8243338341e+01    3.7692572861e+00    2.8544413526e+01
       12     1.8239911218e+01    3.7380180788e+00    2.9567569982e+01
       13     1.7475741553e+01    4.2125567523e+00    2.9161952296e+01
       14     1.6655299108e+01    4.5656273576e+00    2.9643965596e+01
       15     1.6849417087e+01    4.7544734932e+00    6.5061471506e-01
    ...
    and the output from the program is:

    Code:
      ATOM  1         0 0 0
    Clearly I am missing some logic somewhere in the program but I can't see it. I guess I have been staring at this too long. Thanks again for the help

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Code:
    while (infile.get(ch))
    	{
    		infile.get(ch); // I doubt this is what you want
    	}
    You're effectively skipping a character here.
    the get() function reads a character into ch, then returns an istream object, in this case infile. The istream object is then tested in the while loop coniditon (via an overloaded operator, check out the source code) to test if the stream is in "good" status.. in this case, whether a character could be read or not.

    a standard method of reading and processing a character at a time is like this:

    Code:
    while (f.get(c))
    {
        // do something with c
    }
    
    // here, f is either EOF or some other I/O error occured.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while ((c = getchar()) != EOF), AND cntrl z
    By Roger in forum C Programming
    Replies: 8
    Last Post: 10-21-2009, 09:25 PM
  2. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  3. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 04:04 PM
  4. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM
  5. EOF trouble
    By Saiyanman in forum C Programming
    Replies: 6
    Last Post: 03-07-2002, 01:13 PM