?
Solved

Writing to a text file

Posted on 2005-04-08
7
Medium Priority
?
211 Views
Last Modified: 2010-05-18
Hi,

I have a string I'd like to write to a non-existent textfile, and would like to create the file in the process.

Anybody have a few lines of source they can post which will show me how to do this?


Thanks,
Bob
0
Comment
Question by:ba272
  • 3
  • 3
7 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 1500 total points
ID: 13739462
Hi ba272,
You can use fstream to open the file.

#include <fstream>
using namespace std;

fstream file("c:\\Myfile.txt", std::ios::out);

char data[] = "Hello World";
file.write(data, strlen(data));

std::string strData = "Hello World";
file.write(datstrData.c_str(), datstrData.size());


David Maisonave :-)
Cheers!
0
 
LVL 30

Expert Comment

by:Axter
ID: 13739473
FYI:
If you only plan on writing to the file, you can use ofstream instead of fstream
0
 

Author Comment

by:ba272
ID: 13739478
If you could include the few lines needed to read in that text file, it would be appreciated.


Thanks,
Bob
0
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!

 

Author Comment

by:ba272
ID: 13739528
Thanks David.  Would you mind giving me the file read piece as well?
0
 
LVL 30

Expert Comment

by:Axter
ID: 13739541
#include <string>
#include <fstream>
using namespace std;

ifstream file("c:\\Myfile.txt");

std::string strData;

std::getline(file, strData); //Read a line in your text file
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
ID: 13739543
#include <fstream>
#include <string>
using namespace std;

string read_file(char* filename) {

string strLine;

ifstream is(filename);

if ( is.is_open()) {

getline(is,strLine);
}

return strLine;
}

and call that like

string strContent = read_file("c:\\myfile.txt");
0
 

Author Comment

by:ba272
ID: 13739572
Thanks.  You both tied for the second piece, with different approaches.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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…
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 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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

830 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