Inserting strings in middle of a text file using streams I have a text file in a specific format that I pass as the input of my program.  I then want to write the output back to that same file.  The format is as follows:

Input1:
1 2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 7
-----------------------------------------
Input2:
1 2 3 8 5 6 7

1 2 3 9 5 6 7

1 2 3 10 5 6 7
-----------------------------------------
Input3:
1 2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 7


If I want to process Input2 and add the output, the resulting file should be:

Input1:
1 2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 7
-----------------------------------------
Input2:
1 2 3 8 5 6 7

1 2 3 9 5 6 7

1 2 3 10 5 6 7
    output: 1 2 3 4 5 6 7
-----------------------------------------
Input3:
1 2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 7


I am looking for a way to insert lines into that section.  For that I wrote the following routine:

//string fileName: Filename on which read and write operation are being done
//int outputNumber: Number of output to be written
//int nInputLine: How many lines each input is taking
//int nOuputLine: How many lines each output is taking. For now it is not     
//being used
//string outpur: Output to be Written
void CFirstIO::WriteOutPut(string fileName, int outPutNumber, int  
 nInputLine, int nOutPutLine, string outPut)
{
  ifstream fileSt(fileName.c_str(),ios::binary);
      if(fileSt.good() &amp;&amp; fileSt.is_open())
  {
    //Storing the content of the file in a string
    fileSt.seekg(0,ios::end);
    int nLength=fileSt.tellg();
    fileSt.seekg(0,ios::beg);
    char *buff = new char[nLength];
    fileSt.read(buff,nLength);
    string strFileContent(buff); 
    delete []buff;

    string strToFind;
    char formatinput[256];
    sprintf(formatinput,"Input%d:",outPutNumber);
    strToFind+=formatinput;

    fileSt.seekg(0,ios::beg);
    int nPos= strFileContent.find(strToFind,0);
    fileSt.seekg(nPos);      

    //Trying to reach at the bytes Number where I want to insert the 
    //output
    string sLine;
    for(int i=0; i&lt;nInputLine+1;i++)
    {
        if(!fileSt.eof() &amp;&amp; fileSt.good())
        getline(fileSt,sLine);
    }

    nPos=fileSt.tellg();
    strFileContent.insert(nPos,outPut);
    fileSt.close();

    ofstream fileOut(fileName.c_str(),ios::out|ios::trunc);
    fileOut&lt;&lt;strFileContent;

    fileOut.close();      


    }
}   


This function works, but adds some unintended characters at the end of the file when reading the whole file in the buffer.

All in all, this function does not look nice to me as I feel like I'm not using the full potential of the standard library.  How could my code be improved?
First and foremost, fix your formatting.  A little bit of effort spent on making the code readable will mean that it can be understood (and thus reviewed) faster.  By the way, you are not using the STL.

Now, significant issues in order of when they appear in your code:


Use an std::vector in favour of new[] pretty much always.  In this case, as the content is all text, an std::string may be even better, but causes problems due to you wanting to read directly into it.
As you want to work with the input on a line-by-line basis, I would advise an std::vector&lt;std::string&gt; that you can populate using a std::string line; while (fileSt &gt;&gt; 
line) lines.push_back(line);.  There may be more efficient solutions, but check that you really need them first.
What you're doing with sprintf doesn't make sense.  That could all be replaced with std::string strToFind = "Input" + boost::lexical_cast&lt;std::string&gt;(outPutNumber);.
Notice that if you store the strings as I recommended above, finding a certain line is as simple as lines[nInputLine].
To write everything back, just insert another line into the vector and then write it using a loop or with stream iterators.
No need to explicitly close the files unless you care about the exception that may throw.


And some minor issues:


Your comments are inconsistent, and duplicate information.  You have specified the type everywhere except for in filename, despite the type already being given by the signature.
No need to check for fileSt.good() when checking if its open.
As your function only does anything if the file is opened successfully, return early instead of wrapping the whole thing in a giant if.
Be consistent with qualifying things with std::; do it or don't do it, but don't mix it.  (I would advise explicitly qualifying things above using-declarations (and even more so above using-directives), but all three may be acceptable.)
Same with Hungarian notation: even if you don't agree that the qualifiers don't help with anything, you should at least either use them consistently.