problem with ifstream
-
Hi All, I always appreciate your help. I have a problem with ifstream This is my 'input.txt' file. The first digit of each line is the length. How can I read in this file using while() or for() 2 3 4 5 7 8 10 11 12 7 8 10 16 17 54 37 38 3 33 51 48 ... I tried this one. ... ifstream inpf_EX("input.txt"); for (i=1; i<=N_Of_LINE;i++) { inpf_EX>>EXIST[i][0]); len=EXIST[i][0]; for(j=1; j<=len; j++) { inpf_EX>>EXIST[i][j]); } } Thanks! ...
Yonggoo
-
Hi All, I always appreciate your help. I have a problem with ifstream This is my 'input.txt' file. The first digit of each line is the length. How can I read in this file using while() or for() 2 3 4 5 7 8 10 11 12 7 8 10 16 17 54 37 38 3 33 51 48 ... I tried this one. ... ifstream inpf_EX("input.txt"); for (i=1; i<=N_Of_LINE;i++) { inpf_EX>>EXIST[i][0]); len=EXIST[i][0]; for(j=1; j<=len; j++) { inpf_EX>>EXIST[i][j]); } } Thanks! ...
Yonggoo
Yonggoo wrote:
How can I read in this file using while() or for()
Neither are necessary, and their use would only serve to confuse you further. You can use an
iterator
on astringstream
object, and then use thecopy()
function. You don't need an extra digit in the front to tell how many items follow. If it has to stay, however, just read it in and ignore it.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Yonggoo wrote:
How can I read in this file using while() or for()
Neither are necessary, and their use would only serve to confuse you further. You can use an
iterator
on astringstream
object, and then use thecopy()
function. You don't need an extra digit in the front to tell how many items follow. If it has to stay, however, just read it in and ignore it.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
I have not used an iterator on a stringstream. Does std c++ have iterator? Thanks!
Yonggoo
Yonggoo wrote:
Does std c++ have iterator?
Yes.
class LineItem
{
public:
LineItem() {}
~LineItem() {}void setLength(const int length) { \_Length = length; } int getLength() const { return \_Length; } void setNumbers(const std::vector<int>& numbers) { \_Numbers.assign(numbers.begin(), numbers.end()); } std::vector<int> getNumbers() const { return \_Numbers; }
private:
int _Length;
std::vector<int> _Numbers;
};std::ostream& operator<<(std::ostream& os, const LineItem& s)
{
os << s.getLength() << "|";const std::vector<int> numbers = s.getNumbers(); std::copy(numbers.begin(), numbers.end(), std::ostream\_iterator<int>(os, " ")); os << std::endl; return os;
}
std::istream& operator>>(std::istream& is, LineItem& s)
{
int length;
is >> length;std::string sNumbers = ""; std::getline(is, sNumbers); std::stringstream ss(sNumbers); std::vector<int> numbers; std::copy(std::istream\_iterator<int>(ss), std::istream\_iterator<<int>(), std::back\_inserter(numbers)); s.setLength(length); s.setNumbers(numbers); return is;
}
void main( void )
{
std::ifstream fin;
std::vector<LineItem> lines;fin.open("c:\\\\input.txt"); std::copy(std::istream\_iterator<LineItem>(fin), std::istream\_iterator<LineItem>(), std::back\_inserter(lines)); fin.close(); std::copy(lines.begin(), lines.end(), std::ostream\_iterator<LineItem>(std::cout, "\\n"));
}
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne