Thread: file I/O

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    18

    file I/O

    Code:
    void ListFileHDD(ifstream& fin)
    {
    	string line;
    	while (!fin.eof())
    	{
    		getline(fin,line);
    	}
    	cout<<line;
    }
    error C3861: 'getline': identifier not found, even with argument-dependent lookup
    error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)



    I have this function it is not working.

    I want to know how i can read a file and display it to screen.
    Last edited by cmasri; 01-17-2007 at 10:38 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    this will compile
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    void ListFileHDD(std::ifstream& fin)
    {
    	std::string line;
    	while (!fin.eof())
    	{
    		std::getline(fin,line);
    	}
    	std::cout<<line;
    }
    but you should read a FAQ about NOT using eof for controlling the loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Try this:
    Code:
    #include <iostream> 
    #include <fstream> 
    #include <string>
    
    using namespace std;
    
    void readAndDisplay(ifstream& inputFile)
    {
    	string line;
    	while( getline(inputFile, line) )
    	{
    		cout<<line<<endl;
    	}
    }
    
    int main()
    {
    
    	ifstream myInputFile("C:\\TestData\\data.txt");
    	
    	if(!myInputFile)
    
    	{
    		cout<<"couldn't open file"<<endl;
    		return 1;
    	}
    	
    	readAndDisplay(myInputFile);
    
    	return 0;
    }
    The basic rule when you read from files is to make the read statement the while loop conditional.
    Last edited by 7stud; 01-17-2007 at 10:50 PM.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    Quote Originally Posted by vart
    this will compile
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    void ListFileHDD(std::ifstream& fin)
    {
    	std::string line;
    	while (!fin.eof())
    	{
    		std::getline(fin,line);
    	}
    	std::cout<<line;
    }
    but you should read a FAQ about NOT using eof for controlling the loop
    it did not compile...

    error C2039: 'getline' : is not a member of
    error C3861: 'getline': identifier not found, even with argument-dependent lookup
    error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    Quote Originally Posted by 7stud
    Try this:
    Code:
    #include <iostream> 
    #include <fstream> 
    #include <string>
    
    using namespace std;
    
    void readAndDisplay(ifstream& inputFile)
    {
    	string line;
    	while( getline(inputFile, line) )
    	{
    		cout<<line<<endl;
    	}
    }
    
    int main()
    {
    
    	ifstream myInputFile("C:\\TestData\\data.txt");
    	
    	if(!myInputFile)
    
    	{
    		cout<<"couldn't open file"<<endl;
    		return 1;
    	}
    	
    	readAndDisplay(myInputFile);
    
    	return 0;
    }
    The basic rule when you read from files is to make the read statement the while loop conditional.
    getting error C3861: 'getline': identifier not found, even with argument-dependent lookup

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Try removing std::getline().

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    Quote Originally Posted by 7stud
    Try removing std::getline().
    I already did, i'm still stuck

    Code:
    void ListFileHDD(ifstream& fin)
    {
    	string line;
    	while( getline(fin, line) )
    	{
    		cout<<line<<endl;
    	}
    
    
    }

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Were you able to compile the program I posted?

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    Quote Originally Posted by 7stud
    Were you able to compile the program I posted?
    yes it compiles....

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Then in the code I posted, remove:

    using namespace std;

    and then add 'std::' to everything. Do you get the error?

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    what compiler are you using?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  12. #12
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    Quote Originally Posted by vart
    what compiler are you using?
    ms visual studio.net 2003
    Last edited by cmasri; 01-17-2007 at 11:23 PM.

  13. #13
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    Code:
    void ListFileHDD(ifstream& fin)
    {
    	char line[500];
    	
    	
    	while (!fin.eof())
    	{
    		fin.getline(line,'\n');
    	}
    	cout<<line;
    
    
    
    }
    this compiled..

  14. #14
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    Quote Originally Posted by vart
    this will compile
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    void ListFileHDD(std::ifstream& fin)
    {
    	std::string line;
    	while (!fin.eof())
    	{
    		std::getline(fin,line);
    	}
    	std::cout<<line;
    }
    but you should read a FAQ about NOT using eof for controlling the loop
    where is the faq?

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    try to find word FAQ on this pages top
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM