Solved

c++ string conversion

Posted on 2011-03-17
7
338 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:rdeta001
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 35160268
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.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 35160285
I assume the error related to the line

char outfilename[] = outfile;


which is indeed a syntax error - you can do that with a string literal on the right side only, e.g.

char outfilename[] = "Test";

Neveretheless, this assignment is unnecessary, to use 'outfile' within 'open()', just make that

outfile = theEnv->fPath;
outfile = outfile + ".txt";

outstream.open(outfile.c_str(), ios::out);

Open in new window

0
 
LVL 33

Expert Comment

by:sarabande
ID: 35163775
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
 

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Expert Comment

by:evilrix
ID: 35163786
>> 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/
0
 
LVL 33

Expert Comment

by:sarabande
ID: 35163926
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
0
 

Author Comment

by:rdeta001
ID: 35164700
It looks like jkr's soultion may fix the issue, let me give that a shot..
0
 

Author Closing Comment

by:rdeta001
ID: 35165026
Thank you very much, this worked perfectly.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now