?
Solved

Append a string to a file

Posted on 2004-04-27
3
Medium Priority
?
715 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
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 200 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

850 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