C++ c++;,fstream和seekp和seekg,奇怪的程序
我正在尝试使用fstream来尝试同时读写。我读了第一行,然后写了文件最后一句话C++ c++;,fstream和seekp和seekg,奇怪的程序,c++,visual-studio-2010,C++,Visual Studio 2010,我正在尝试使用fstream来尝试同时读写。我读了第一行,然后写了文件最后一句话 #include <iostream> #include <fstream> #include <string> using namespace std; int main(){ int pos1=0,pos2=0; string cadFile; fstream fs("ejemplo.txt"); do{ fs.see
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
int pos1=0,pos2=0;
string cadFile;
fstream fs("ejemplo.txt");
do{
fs.seekg(pos1,ios::beg);
getline(fs,cadFile);
pos1=fs.tellg();
fs.seekp(pos2,ios::end);
fs<<cadFile;
pos2=fs.tellp();
cout<<pos1<<"-"<<pos2<<"-"<<cadFile<<endl;
}while(!fs.eof());
fs.close();
system("pause");
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
int pos1=0,pos2=0;
字符串文件;
fstream fs(“ejempo.txt”);
做{
财政司司长seekg(职位1,ios::beg);
getline(fs,cadFile);
pos1=fs.tellg();
财政司司长seekp(位置2,ios::结束);
这就是解决方案:int main(){
int pos1,pos2,temp;
字符串文件;
温度=1;
pos1=pos2=0;
fstream fs(“ejempo4.txt”,ios::in | ios::out | ios::app);
财政司司长seekg(职位1,ios::beg);
while(getline(fs,cadFile)&&pos1这是解决方案:int main(){
int pos1,pos2,temp;
字符串文件;
温度=1;
pos1=pos2=0;
fstream fs(“ejempo4.txt”,ios::in | ios::out | ios::app);
财政司司长seekg(职位1,ios::beg);
while(getline(fs,cadFile)&&POS1为什么不使用ifstream
和ofstream
呢?是的,我可以使用它,但是当fstream可以同时读写时,我必须创建两个对象。我不知道当我想在同一个文件中读写时是否可以工作。我想我需要使用seekp和seekg。这是解决方案:如果它们有效,为什么不使用ifstream
和ofstream
呢?是的,我可以使用它,但当fstream可以同时读写时,我必须创建两个对象。我不知道当我想在同一个文件中读写时是否有效。我认为我需要使用seekp和seekg。这是解决方案:
int pos1,pos2,temp;
string cadFile;
temp=1;
pos1=pos2=0;
fstream fs("ejemplo4.txt",ios::in|ios::out|ios::app);
fs.seekg(pos1,ios::beg);
while(getline(fs,cadFile)&&pos1<temp){
pos1=fs.tellg();
fs.seekp(pos2,ios::end);
if(pos2==0) temp=fs.tellp();
fs<<cadFile<<endl;
pos2=fs.tellp();
cout<<pos1<<"-"<<pos2<<"-"<<cadFile<<endl;
fs.seekg(pos1,ios::beg);
};
fs.close();
system("pause");
return 0;