1

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");
}
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
  • 1
    A few questions... (1) Why are there double quotes in your includes? Those are standard library names, unless you're redefining things. Either way, those are confusing names to go with. (2) Why both iostream and stdio.h? This is C++, not C. has no place in C++. – Parthian Shot Apr 27 '15 at 00:01
  • @CaptainObvlious he did put a `j++` in his code The problem is more that you don't clearly explain what isn't working. Anyway the first thing you should correct is explicitly closing `file1` after you're done with it. – meneldal Apr 27 '15 at 01:54

1 Answers1

0

To replace you can use replace method of std::string, but you need to know range of characters to replace (to find it use find method). I found good approach in this answer. Also it is better to use std::string and don't mess it with char and char arrays, so to convert int to std::string I used another answer, or you can simply use std::to_string, if it's acceptable for you.

So here is full code, it will create 9 new files as-10.txt, as-2.txt, as-3.txt, as-4.txt, as-5.txt, as-6.txt, as-7.txt, as-8.txt and as-9.txt.

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

namespace patch {
    template< typename T >
    std::string to_string( const T& n ) {
        std::ostringstream stm;
        stm << n;
        return stm.str();
    }
}

using namespace std;

void replace_all(string& str, const string& from, const string& to) {
    if(from.empty())
        return;
    size_t start_pos = 0;
    while((start_pos = str.find(from, start_pos)) != string::npos) {
        str.replace(start_pos, from.length(), to);
        start_pos += to.length();
    }
}

int main(int argc, char** argv) {
    ifstream myfile("as-1.txt");
    if(!myfile) {
        cerr << "Could not open file " << "as-1.txt\n";
        return 1;
    }
    for(int i = 0; i < 9; i++) {
        string name = "as-" + patch::to_string(i + 2) + ".txt";
        ofstream myfile1(name.c_str());
        if(!myfile1) {
            cerr << "Could not open file " << name << "\n";
            return 1;
        }
        cout << name<<endl;
        string line;
        while(!myfile.eof()) {
            getline(myfile, line);
            replace_all(line, "as-1", "as-" + patch::to_string(i + 2));
            myfile1 << line << endl;
        }
        myfile.clear();
        myfile.seekg(0, ios::beg);
    }
    return 0;
}
Community
  • 1
  • 1
Nikolay K
  • 3,770
  • 3
  • 25
  • 37