Solved

Append a string to a file

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now