CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Create ifstream_iterator error

    Hello to all, i would like to create ifstream_iterator to use it with file.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <list>
    #include <iterator>
    
    using namespace std;
    
    
    int main()
    {
    	string dataFile("C:\\Documents and Settings\\nicholas_tse\\Desktop\\.Tc.txt");
    	/* Wrong 
    
    	This declare a function taking two parameter
    	and return list<int> due to first paremeter
    	is ignore and second parameter is pointer
    	to function
    
    	Remember 
    	1. Parentheses around name is ignore
    	2. Parentheses has empty argument is function
    
    	*/
    
    /*
    	list<int> inputFile(istream_iterator<int>(dataFile), 
    		istream_iterator<int>());
    */
    	// Solution 1
    /*	list<int> nextInputFile((istream_iterator<int>(dataFile)), 
    		istream_iterator<int>());*/
    
    	// Solution 2
    	ifstream input(dataFile.c_str());
    	istream_iterator<string> start;
    	istream_iterator<string> end;
    	list<string> strList(start, end);
    
    
    	// No default constructor available
    //	ostreambuf_iterator<int> outBufferIte; 
    	istreambuf_iterator<int> inBufferIte;   
    
    	// No default constructor available
    //	ostream_iterator<int> outIte; 
    	istream_iterator<int> inIte;
    	
    
    	return 0;
    }
    Question

    1. Why this is list<int> inputFile(istream_iterator<int>(dataFile),
    istream_iterator<int>()); wrong ?
    2. Why i try solution 1 there are error list<int> nextInputFile((istream_iterator<int>(dataFile)),
    istream_iterator<int>()); ?

    error C2440: '<function-style-cast>' : cannot convert from 'std::string' to 'std::istream_iterator<_Ty>'

    3. How to create ifstream_iterator to use it with file processing ?

    4. Why compiler prohibited me to create ostreambuf_iterator<int> outBufferIte;
    ostream_iterator<int> outIte; ?

    Thanks.
    Thanks for your help.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Create ifstream_iterator error

    #1) I'm not sure what your question is. Your code comments state
    why it doesn't do what you want.

    #2) dataFile is a std::string ... it should be an ifstream

    Note concerning your solution # 2 ... it should be

    Code:
    istream_iterator<string> start(input);
    #3 and #4 : see example below

    Code:
    #include <iostream>
    #include <fstream>
    #include <iterator>
    
    using namespace std;
    
    void Create()
    {
       ofstream out("ints.txt");
       out << " 1 2 3 4 5 6 7 8 9 10\n";
    }
    
    
    int main()
    {
        Create();
    
        ifstream in("ints.txt");
    
        istream_iterator<int> inpos(in);
        istream_iterator<int> end_pos;
    
        ostream_iterator<int> outpos(cout,"\n");
    
        while (inpos != end_pos)
        {
           *outpos = *inpos;
           ++inpos;
           ++outpos;
        }
    
        return 0;
    }

  3. #3
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Create ifstream_iterator error

    Quote Originally Posted by Peter_APIIT View Post
    Question

    1. Why this is list<int> inputFile(istream_iterator<int>(dataFile),
    istream_iterator<int>()); wrong ?
    Because the compiler will mistake this for a function delcaration for a function called inputFile that returns an object of type list<int> and takes two parameters, the first being a parameter of type istream_iterator<int> called dataFile and the second being a function pointer of no name that takes no parameters and returns an object of type istream_iterator<int>. In order to prevent the compiler mistaking your intended list object declaration as a function declaration, you have to add parenthesis around one or both of the formal parameter declarations. e.g.

    Code:
    list<int> inputFile((istream_iterator<int>(dataFile)), istream_iterator<int>());
    Code:
    list<int> inputFile(istream_iterator<int>(dataFile), (istream_iterator<int>()));
    Quote Originally Posted by Peter_APIIT View Post
    2. Why i try solution 1 there are error list<int> nextInputFile((istream_iterator<int>(dataFile)),
    istream_iterator<int>()); ?

    error C2440: '<function-style-cast>' : cannot convert from 'std::string' to 'std::istream_iterator<_Ty>'
    As Philip has said, your data file is of type std::string, when it should derive from std::istream.

    Quote Originally Posted by Peter_APIIT View Post
    3. How to create ifstream_iterator to use it with file processing ?
    Use the code Philip has given you or alternatively the following will do the same thing:
    Code:
    ifstream dataFile("ints.txt");
    list<int> inputFile(istream_iterator<int>(dataFile), (istream_iterator<int>()));

  4. #4
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Create ifstream_iterator error

    Code:
    string nextInputFile((istream_iterator<string>()), 
    		(istream_iterator<string>()));
    Compiler pops up this error.

    error C2440: 'type cast' : cannot convert from 'const std::string' to 'char'

    Why this statement cause the error ?
    Thanks for your help.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Create ifstream_iterator error

    You are trying to initialise a std::string object named nextInputFile with a pair of input stream iterators that denote a range. These iterators iterate over std::strings. However, the iterators that should be used for the initialisation should iterator over chars. Thus, you get the error saying that the conversion from std::string to char could not be done.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    May 2009
    Posts
    4

    Re: Create ifstream_iterator error

    i think you may need some head files ,i can get through complie with some needed head files
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <list>
    #include <iterator>
    using namespace std;

  7. #7
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Create ifstream_iterator error

    Quote Originally Posted by Peter_APIIT View Post
    Code:
    string nextInputFile((istream_iterator<string>()), 
            (istream_iterator<string>()));
    Compiler pops up this error.

    error C2440: 'type cast' : cannot convert from 'const std::string' to 'char'

    Why this statement cause the error ?
    I expect you probably meant
    Code:
    string nextInputFile(istream_iterator<char>(istreamObj), (istream_iterator<char>()));
    where istreamObj is an istream derivative such as fstream, ifstream, stringstream, istringstream etc.

    However, if you wanted to put each delimited character sequence (e.g. each word) into a separate element in a container then you could do the following:

    Code:
    vector<string> nextInputFile(istream_iterator<string>(istreamObj), (istream_iterator<string>()));
    The above yeilds a vector of strings, where each string element contains a delimited character sequence (e.g. a single word).

  8. #8
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Create ifstream_iterator error

    Thanks.
    Thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured