file I/O

This is a discussion on file I/O within the C++ Programming forums, part of the General Programming Boards category; Code: void ListFileHDD(ifstream& fin) { string line; while (!fin.eof()) { getline(fin,line); } cout<<line; } error C3861: 'getline': identifier not found, ...

  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 09:38 PM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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 09: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,662
    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,662
    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,662
    Then in the code I posted, remove:

    using namespace std;

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

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    what compiler are you using?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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 10: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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    try to find word FAQ on this pages top
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
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, 08:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01: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, 04:56 AM

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