Solved

c++ string conversion

Posted on 2011-03-17
7
339 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

786 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