Chào anh chị! Em mới học đến phần seekg và seekp thì ko hiểu sự khác nhau giữa 2 hàm này là gì?
Em có code 1 đoạn chương trình (mở để đọc) nhưng vị trí trả về cho thấy hàm seekg (đã thử cả seekp không hoạt động, 1 đoạn code mở để ghi thì hàm seekg có hoạt động.
Mọi giải thích em với ạ! Em cảm ơn
#include <iostream>
#include <string>
#include<fstream>
using namespace std;
int main()
{
fstream f;
f.open("vidu.txt",ios::in);
string str[20];
int i=0;
while(!f.eof()){
getline(f,str[i],'|');
i++;
}
cout<<"\nVi tri 1: "<<f.tellg();
string s[10];
f.seekg(2,ios::beg);
cout<<"\nVi tri 2: "<<f.tellg();
getline(f,s[0],'|');
cout<<endl<<s[0]<<endl;
f.close();
fstream f1;
f1.open("vidu.txt",ios::out); // mo de ghi
for(int j=0;j<i;j++){
if(j!=i-1)
{
if(j%2==0 && j!=0)
f1<<endl;
f1<<str[j]<<"|";
}
else
f1<<str[j];
}
f1.seekg(4,ios::beg);
cout<<"\nVi tri 3: "<<f1.tellg();
f1.close();
return 0;
}