这段代码的目的是以流的形式打开一个文件,并把文件转为字符串保存到一个文本文件中.
可以运行,但是得到的文件没有发生变化,还是原来的那个文件.请教高手指点.
没写注释麻烦大家了.感谢wfpb的帮助.
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
class MyFstream
{
vector<char> file;
int m_nTotalChar;
public:
MyFstream():m_nTotalChar(0){};
void GetFromFile(const char *filename);
void PutToFile(char *filename);
};
void MyFstream::GetFromFile(const char *filename)
{
char ch;
ifstream ins(filename ,ios_base::binary);
while (!ins.eof())
{
ins.get(ch);
file.push_back(ch);
m_nTotalChar++;
}
ins.close();
}
void MyFstream::PutToFile(char*filename)
{
const char*p=".txt";
strcat(filename,p);
ofstream ous(filename);
for (int i=0;i<m_nTotalChar;i++)
{
ous.put(file[i]);
}
ous.close();}
int main(int argc, char* argv[])
{
char *pSourceFile=new char[20];
p=pSourceFile;
char *pAimFile=new char[20];
cout<<"Input the source-filename:\n";
cin>>pSourceFile;
cout<<"Input the aim-filename:\n";
cin>>pAimFile;
MyFstream ms;
ms.GetFromFile(pSourceFile);
ms.PutToFile(pAimFile);
delete pAimFile;
delete pSourceFile;
return 0;
}