-
June 2nd, 2009, 04:16 AM
#1
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.
-
June 2nd, 2009, 07:54 AM
#2
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;
}
-
June 2nd, 2009, 12:06 PM
#3
Re: Create ifstream_iterator error
Originally Posted by Peter_APIIT
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>()));
Originally Posted by Peter_APIIT
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.
Originally Posted by Peter_APIIT
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>()));
-
June 3rd, 2009, 04:03 AM
#4
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.
-
June 3rd, 2009, 10:00 AM
#5
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.
-
June 3rd, 2009, 10:34 PM
#6
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;
-
June 4th, 2009, 04:08 AM
#7
Re: Create ifstream_iterator error
Originally Posted by Peter_APIIT
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).
-
June 5th, 2009, 05:10 AM
#8
Re: Create ifstream_iterator error
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|