Solved

c++ string conversion

Posted on 2011-03-17
7
337 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
Comment Utility
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
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 40

Expert Comment

by:evilrix
Comment Utility
>> 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 32

Expert Comment

by:sarabande
Comment Utility
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
Comment Utility
It looks like jkr's soultion may fix the issue, let me give that a shot..
0
 

Author Closing Comment

by:rdeta001
Comment Utility
Thank you very much, this worked perfectly.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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

11 Experts available now in Live!

Get 1:1 Help Now