question about file input

I am reading some number matrix from a text file . I had two pieces of code
1:
ifstream ifs;
//open file
while(!ifs.eof()){
//getline & other stuff
}

2:
ifstream ifs;
//open file
while(getline(ifs,some_string_variable){
//other stuff
}

The first one always get one duplicate of the last number while the second works great. Can someone explain the difference here?
Ummmm perhaps include what that "other stuff" is? Otherwise, it is not very clear why the while loops are not the exact same.
Also there seems to be a missing ) in the second one.
Last edited on
this works for me..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char* argv[]) {
	
	ifstream file("myfile.txt");
	string buf;
	while(!file.eof()) {
		getline(file, buf);
		cout << buf << endl;
	}

	file.close();
	return 0;
}
[myfile.txt]
this
is
a
file
testing
program
using
ifstream
in
c++
programming
language


and please enclose you code with [code][/code] to be read easily..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
using namespace std;

int main(){
    ifstream ifs;
    ifs.open("test");
    int n;
    while (!ifs.eof()){
	ifs>>n;
	cout<<n<< " " ;
    }
    cout << endl;
    ifs.close();
    return 0;
}
output: 
1 1 1 1 1

test:
1 1
1 1
There is one too many 1 in the output
another reason is maybe because he is using binary mode.. ios::binary
Topic archived. No new replies allowed.