Solved

Append a string to a file

Posted on 2004-04-27
3
702 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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
c++ how to tell if the progra is ctl or mfc atl ect 6 107
C++ standard library based binary archive format 6 109
VS2015 Redefinition errors 4 100
I could not set window to top 4 59
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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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…

742 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