从结果中
--0 0
--1 2
--2 20
--3 0
BONJOUR MA BICHETTE
--4 -1
is_open
--5 -1
--6 -1
7 20
8 0
------- C CODE----------
++1 0
++2 20
++3 0
->BONJOUR MA BICHETTE
feof encountered
++4 20
++5 0
Program ended with exit code: 0
为什么看起来
-1 2
与
ifsA.seekg(0,std :: ios_base :: end);
代替
-1 20(因为文件大小为20!)
为什么看不到任何东西
如果(fsA.eof())
为什么需要做
ifsA.clear();
获得
7 20
它很失望
#include
#include
使用命名空间标准;
int main(int argc,const char * argv []){
const char * c =“ / Users / uio / Desktop / C ++ _ PROB_SEEKG / C ++ _ PROB_SEEKG / 1.txt”;
ifstream ifsA(c);
if(ifsA){
字符串ligne;
cout <<“-0” << ifsA.tellg()<< endl;
ifsA.seekg(std :: ios_base :: end);
cout <<“-1” << ifsA.tellg()<< endl;
ifsA.seekg(0,std :: ios_base :: end);
cout <<“-2” << ifsA.tellg()<< endl;
ifsA.seekg(ios_base :: beg);
cout <<“-3” << ifsA.tellg()<< endl;
while(getline(ifsA,ligne)){
cout << ligne << endl;
}
cout <<“-4” << ifsA.tellg()<< endl;
if(ifsA.is_open())
cout <<“ is_open” << endl;
ifsA.seekg(ios_base :: beg);
cout <<“-5” << ifsA.tellg()<< endl;
ifsA.seekg(0,ios_base :: beg);
cout <<“-6” << ifsA.tellg()<< endl;
if(ifsA.eof())
cout <<“ eof” << endl;
//https://stackoverflow.com/questions/16364301/whats-wrong-with-the-ifstream-seekg
ifsA.clear();
cout <<“ 7” << ifsA.tellg()<< endl;
if(ifsA.eof())
cout <<“ eof” << endl;
ifsA.seekg(ios_base :: beg);
cout <<“ 8” << ifsA.tellg()<< endl;
}
否则{
cerr <<“ ifstream !!” << endl;
出口(1);
}
puts(“ ------- C代码----------”);
#include
FILE * f = fopen(c,“ r”);
char buf [BUFSIZ];
如果(f == NULL){
perror(“ fopen”);
出口(1);
}
否则{
printf(“ ++ 1%ld \ n”,ftell(f));
fseek(f,0,SEEK_END);
printf(“ ++ 2%ld \ n”,ftell(f));
fseek(f,0,SEEK_SET);
printf(“ ++ 3%ld \ n”,ftell(f));
while((fgets(buf,BUFSIZ,f)!= NULL){
printf(“->%s \ n”,buf);
}
if(feof(f)){
printf(“%s \ n”,“遇到的脚步”);
}
printf(“ ++ 4%ld \ n”,ftell(f));
fseek(f,0,SEEK_SET);
printf(“ ++ 5%ld \ n”,ftell(f));
fclose(f);
}
返回0;
}