reaching EOF question

hi,

this does not work for me:

1
2
3
4
5
6
7
8
ifstream object;
object.open("file.txt");
//...cheking OK
object.seekg (0, ios::end); //ERROR
//...
char ch;
object.get(ch);
//.... 

error, I can't read last char.

my general question is why can't i set value 0 to the seekg function?
I know it works with object.open("file", ios::ate); but that's not a qustion now, plese help.

thanks!
hi,
with ios::end this function takes first parameter as negative value.

object.seekg (-1, ios::end);
tnx for your reply,
object.seekg (-1, ios::end); works fine, but would like to understand what does exactly mean when we pass 0:
object.seekg (0, ios::end);

thanks.
on linux object.seekg (0, ios::end); retuns $ (it marks end of line in ascii file).
it marks end of line in ascii file

sorry if I'm being boring but if it returns end of line which is '\n' then why can't we copy it?
'\n' character is just like any other so it's not an error :|?

thanks so much.
'\n' is interpreted as new line not end of line.
Last edited on
one thing i dont understand that why are positioned the
object.seekg (0, ios::end); //ERROR

to the begining imedialty after when you check for the file open .. when the file is open it is suppose to be at the begning of the file .
Correct me if i am wrong .
@bluecoder
hi, I'm not shore if I understand what are you saying but:
1
2
object.seekg (0, ios::beg); //this works, put the "file pointer" to the begining
object.seekg (0, ios::end); //this is same as above but "file pointer should point to the end 


first line works just fine and is supposted to move "file pointer" to the beginging when your file pointer is somewere else in the file.
second line is eqivalent to the first but it's not working in same way as first line does and I don't know why?
second line moves file pointer to the end.
thanks.

@Rishi Rai
'\n' is interpreted as new line not end of line.


woot?
I didn't know that lol

--------
is there some good refecernce to learn about files and symbols like EOF EOL and similar?
thanks.
thanks ... codekiddy this clears my doubt..
ok...
i am creating file1.txt with this code and i am not using '\n'. if this file will be used by your program what will be output?

1
2
3
4
5
6
7
8
9
10
11
#include <fstream>

using namespace std;

int main()
{
ofstream f1;
f1.open("file1.txt");
f1<<"aaaa";
f1.close();
}

just a line "aaaa" in the file . but what is your point Rishi
Last edited on
'\n' is new line not end of file or end of file.
correct me if i am wrong.
yes Rishi you are right .. thanks .
so at the end of file last line in the file at the end is EOL and EOF in same time while all other lines ends are EOL only.
that means there is no char at EOL or EOF and that the reason why object.seekg (0, ios::end); does not work(but works for taking length of the file in bytes)
thanks for explanation.
Topic archived. No new replies allowed.