Hi Guys

I am new at this, and I despeartely require some help.
I basically need to write a program to read a text file, alter a single value and write it to a new file.
I have done some stuff so far which seems to be correct, however, nothing gets written to the new file and no output is displayed.
I'm using C++ Builder 5

Please help...

Thanks

Here is what I have so far:

//---------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vcl.h>
#include <stdio.h>
#include  <string>
#include <stdlib.h>
#include <vcl.h>
#pragma hdrstop

#include "Version.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------



using namespace std;

void Open_Files()
{
    ifstream inFile;
    ofstream outFile;


    inFile.open("c:\TEMP\Patch27_Version.txt", ios::in);
    outFile.open("c:\TEMP\temp.txt", ios::out);

    // check if file opens
    if (!outFile.eof())
    {
        cout << "Unable to open file";
        exit(1); // terminate with error
    }

    // check if file opens
    if (!inFile.eof())
    {
        cout << "Unable to open file";
        exit(1); // terminate with error
    }

   }


void Close_Files()
{

    ifstream inFile;
    ofstream outFile;
   
    outFile.close();
    inFile.close();
}

//converts a string to it's relevant integer value
int GetIntVal(string strConvert)
{
 int intReturn;
 intReturn = atoi(strConvert.c_str());
 return(intReturn);
}


//converts an integer to it's relevant string value
string IntToString(int intValue)
{
  char *myBuff;
  string strRetVal;

  // Create a new char array
  myBuff = new char[100];
  // Set it to empty
  memset(myBuff,'\0',100);
  // Convert to string
  itoa(intValue,myBuff,10);
  // Copy the buffer into the string object
  strRetVal = myBuff;
  // Delete the buffer
  delete[] myBuff;
  return(strRetVal);
}

int main()
{
    ifstream inFile;
    ofstream outFile;
    string line;
    string a = " . " , b = "' ", str;
    size_t first_found, last_found;
    int  num;

    Open_Files();


    //reads file line by line and checks for the first and last occurence of apostrophe and then substrings
    //whatever number is between it, it then converts the number to double and increments the decimal
    if (inFile.is_open())
    {
      while (!inFile.eof())
      {
              getline(inFile,line);
              inFile >>line;
              int (first_found) = line.find_first_of(a);
              int (last_found) = line.rfind(b);
              str =line.substr(first_found+1,last_found-1);
              num = GetIntVal(str);
              num++;
              str = IntToString (num);
              outFile <<"UPDATE DSAFESETTING SET VALUE = '27."  <<str<<"' WHERE DESCRIPTION = 'Patch Version'" ;


      }

     }
     else
     {
       cout << "These damn files won't Open!!!!!!!!!!!!";
      }

    Close_Files();
    return 0;
}

Next time use code tags.

The two streams are local to Open_Files() function, so they are immediately closed as soon as that function terminates. What you want to do is pass the stream by reference as parameters to that function
void Open_Files(ifstream& inFile, ofstream& outFile)

The similar fix for Close_Files()function. And the function you posted is 100% wrong.

void Close_Files(ifstream& inFile, ofstream& outFile)
{
    inFile.close();
    inFile.clear();
    outFile.close();
    outFile.clear();
}

Now down in main() you have to add parameters to the above two functions.

Thanks.
I eventually got it working.
Appreciate your help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.