Link to home
Start Free TrialLog in
Avatar of rdeta001
rdeta001

asked on

c++ string conversion

This has been bothering me for a while and c++ is not my strongest language so I'm hoping someone can help me out with this.  

What I need to do is open a text file, perform some modifications (change all lines that contain only a single period to a single period followed by a space), then save the file with changes.

If there is a better way to make the changes to the text file, I am all for it.  This is what I found and I was able to get it to work if I set the temporary file to something fixed, say "temp.txt", however I need this name to be unique, and theEnv->fPath will always give me a unique file name.

The error I get is: cannot convert from 'const char*' to 'char []'
If I use the commented line instead, I get a similar error converting from std::string to char[]

Again my c++ is pretty weak, any help would be greatly appreciated.


//theEnv->fPath provides the path to the file I need to modify
...
string inbuf;
fstream instream(theEnv->fPath,ios::in);
ofstream outstream;
string outfile;
outfile = theEnv->fPath;
outfile = outfile + ".txt";
char outfilename[] = outfile;
//char outfilename[] = outfile.c_str();
outstream.open(outfilename, ios::out);
while(!instream.eof())
{
   getline(instream, inbuf);
   if (inbuf.length() ==1)
   {
      if (inbuf[0] == '.') inbuf = ". ";
   }
   outstream << inbuf << endl;
}
instream.close();
outstream.close();
int result;
result = remove(theEnv->fPath);
if (result == 0) printf("Delete ok.");
else printf("Delete error.");
result = rename(outfilename, theEnv-fPath);
if (result==0) printf("Rename ok.");
else perror("Rename error.");
...

Open in new window

Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

You really only have 2 choices when it comes to standard C++...


1. Create a temporary, save changes to this then rename it (this is what you are doing)
2. Read data into memory and close file, change in memory then reopen file as output truncating and save.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
for point (2) of the choices evilrix has pointed out, you would add a vector of strings to your program

#include <vector>
...

  std::vector<std::string> all_lines;

Open in new window


and store all changed text lines into the vector.

  while (getline(instream, inbuf))
  {
      if (inbuf == ".")
          inbuf = ". ";
      all_lines.push_back(inbuf);

Open in new window



after loop close the input file and reopen it as ofstream as explained by evilrix, that would truncate the file to size 0. you then would iterate the vector and write all lines back to the file. finally close the output file and all was done.

Sara
 

>> reopen it as ofstream as explained by evilrix, that would truncate the file to size 0
You  need to pass it the std::ios::trunc openmode flag when you open the file to achieve this.

http://www.cplusplus.com/reference/iostream/ios_base/openmode/
http://www.cplusplus.com/reference/iostream/ofstream/
truncation is default for a text file cause file positions at 0 when opening it.

you explicitly would need the std::ios_base::app (append) openmode in case you would want to write at end.

Sara
Avatar of rdeta001
rdeta001

ASKER

It looks like jkr's soultion may fix the issue, let me give that a shot..
Thank you very much, this worked perfectly.