little trouble with getline

This is a discussion on little trouble with getline within the C++ Programming forums, part of the General Programming Boards category; hi, Cant seem to figure out why its returning NULL on getline everytime it iterates when the file (a .obj) ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    28

    little trouble with getline

    hi,

    Cant seem to figure out why its returning NULL on getline everytime it iterates when the file (a .obj) does contain data on its lines.

    the reading function is:

    Code:
    void Loader::Complete_Read() {
    
    	string data = "";
    	bool cont = true;
    
    	if(fin.is_open()) //If file is open
    	{ 	
    		while(cont) //While not finished
    		{
    			if(!(getline(fin, data) == NULL)) //is it EOF
    			{ 	
    				if(!Skip_Line(data)) //is it White line or Skip line
    				{
    					Set_Data(data);  // data handled elsewhere
    				}
    			}	
    			else {
    				
    				Console_Message(0);
    
    				if(fin.is_open()) {
    
    					try { 
    						
    						fin.close();
    
    						cerr << "Closed File safely" << endl;
    					}
    					catch(...) {Console_Message(2); exit(-1);}
    				}		
    				cont = false;
    			}
    		}
    	}
    	else
    	{
    		Console_Message(1);
    		exit(-1);
    	}
    }
    data in .obj example is

    ####
    #
    # OBJ File Generated by LightWave3D
    # LightWave3D OBJ Export v2.2
    #
    ####
    # Object: 1
    #
    # Vertices: 12
    # Points: 0
    # Lines: 0
    # Faces: 7
    # Materials: 1
    #
    ####

    o 1

    # Vertex list

    v -0.539683 0 0.031746
    v 0.52381 0 0.031746

    etc.

    Any help as to what im doing wrong would be great, thankyou :(

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    getline returns the input stream (in this case fin) which is converted to a void* which can be compared as a boolean value or against NULL, although it would be clearer to just use:
    Code:
    if(getline(fin, data))
    Check fin, not fin.is_open() in the first if statement. Also output data to cerr or cout or something to see if anything is read in.

    It is possible that you have already done something with fin that has set an error state, and that error state needs to be cleared before calling getline.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  3. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 10:46 PM
  4. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 01:59 PM
  5. getline trouble
    By endo in forum C++ Programming
    Replies: 4
    Last Post: 08-04-2002, 03:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21