Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;,fstream和seekp和seekg,奇怪的程序_C++_Visual Studio 2010 - Fatal编程技术网

C++ c++;,fstream和seekp和seekg,奇怪的程序

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

我正在尝试使用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.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;