Following is the file (as-1.txt
) in which I must make changes. Instead of as-1
there should be as-2
.
This code makes copies of as-1.txt
and also changes internal data of that file. If the file name is as-2
inside the file, then as-1
should also be changed to as-2
.
I've done the copying part but am unable to change the file data:
Sample input:
&cntrl
pTopt = 298.15, pdens = 0.997, prcut = 12.0, pion = t,
pihot = t, prQM = 5.5, prSM = 5.3, prQI=3.0, piguess = f,
pinit = t, pnstep = 5000, pnscale = 100,
pnstat = 5, pnout = 5, pnrst = 5, pioutc = t, pioutv = t, pnoutc = 5, pnoutv = 5,
msolute = t, nosa = 1, pichrg = t
gfileOut = 'as-1.out',
gfileEnout = 'as-1.en',
gfileInfo = 'as-1.info',
gfileStart = 'init.in',
gfileRst = 'as-1.rst',
gfileTraj = 'as-1.traj',
gfileVeloc = 'as-1.vel',
gfileQmen = 'as-1.qmen'
&end
My code:
#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "windows.h"
#include "stdio.h"
#include "string"
using namespace std;
void main()
{
string str;
ifstream myfile("as-1.txt");// for reading a file
char j = '2';
if (!myfile)// if file not present
{
cerr << "file not opening";//cerr is for displaying errors
exit(1);
}
for (int i = 0; i < 9; i++)// for multiple copies
{
char name[9] = { 'a', 's', '-', j,'.','t','x','t' ,'\0'};// for setting file name , j increments
ofstream myfile1(name);// to write on file
cout << name<<endl;
while (!myfile.eof())// run till end of file
{
getline(myfile,str); // get line by line text from myfile
myfile1 << str << endl;// insert line fetched from myfile to myfile 1
}
j++;// to increment in ascii code
myfile.clear();// to clear end of file(eof)
myfile.seekg(0, ios::beg);//take cursor to begining of file
}
system("pause");
}