Solved

c++ string conversion

Posted on 2011-03-17
7
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 34

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

752 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