Solved

Append a string to a file

Posted on 2004-04-27
3
705 Views
Last Modified: 2008-03-03
Hello I am trying to create a string variable and append that variable on another line of an open file.

Here is what I have so far

     strftime(var, 30, "%m%d", tm_data);
     string tmpfilename(var);
     journalfile = "C:\\hlmkejrnl\\" + tmpfilename + ".txt";

     // open file with a MMDDhhmmss.txt format to save transaction
        ofstream outfile (journalfile.c_str(), ios::app);

         string ejrnlfile = "Hope this works";
          outfile.write(ejrnlfile);
          outfile.close();

"Hope this works" should append to the file MMDD.txt file, but I get an error

Cannot convert 'string' to 'const char *' in function main()

and

Type mismatch in parameter 's' (wanted 'const char *', got 'string') in function main()

Maybe there would be a better way to open a file and append a string to it?  

Any suggestions?

Thanks,

jmsloan
0
Comment
Question by:jmsloan
[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
3 Comments
 
LVL 3

Author Comment

by:jmsloan
ID: 10929887
I believe I figured it out.  I did this and it seems to work

     strftime(var, 30, "%m%d", tm_data);
     string tmpfilename(var);
     journalfile = "C:\\hlmkejrnl\\" + tmpfilename + ".txt";

     // open file with a MMDDhhmmss.txt format to save transaction
        ofstream outfile (journalfile.c_str(), ios::app);

         string ejrnlfile = "Hope this works\n";
         outfile << ejrnlfile;
         outfile.close();
0
 
LVL 30

Accepted Solution

by:
Axter earned 50 total points
ID: 10929900
>>Cannot convert 'string' to 'const char *' in function main()

Do the following instead:
outfile << ejrnlfile;
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 10930447
Axter and jmsloan are absolutely right, but for the record if you do want a pointer to a char* for a legacy C function, you should copy the std::string into std::vector<char> and '\0'-terminate it.

e.g.
--------8<--------
#include <cstdio> // For yucky old printf
#include <string>
#include <vector>

int main()
{
      std::string str = "Hello world";

      std::vector<char> v(str.begin(),str.end());
      v.push_back('\0'); // C strings need '\0' termination

      std::printf("C programmers like to say \"%s\"",&v[0]);
}
--------8<--------
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

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…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

688 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