How to write string to a text file in C++

3 Answers

0 votes
#include <iostream>
#include <fstream>

int main()
{
	std::ofstream afile;

	afile.open("d:\\data.txt");
	afile << "c++ programming" << std::endl;
	afile.close();

	return 0;
}

/*
run:

data.txt
--------
c++ programming

*/

 



answered Nov 15, 2017 by avibootz
0 votes
#include <iostream>
#include <fstream>

int main()
{
	std::ofstream afile;

	afile.open("d:\\data.txt");
	if (afile.is_open())
	{
		afile << "c++ programming language" << std::endl;
		afile.close();
	}
	else std::cout << "Erroe open file";

	return 0;
}

/*
run:

data.txt
--------
c++ programming language

*/

 



answered Nov 16, 2017 by avibootz
0 votes
#include <iostream>
#include <fstream>
#include <string>

int main()
{
	std::string s = "c++ programming";
	
	std::ofstream out("d:\\data.txt");
	out << s;
	out.close();

	return 0;
}

/*
run:

data.txt
--------
c++ programming

*/

 



answered Nov 16, 2017 by avibootz

Related questions

1 answer 232 views
1 answer 185 views
185 views asked Jan 29, 2023 by avibootz
1 answer 258 views
1 answer 283 views
283 views asked Jul 15, 2015 by avibootz
1 answer 312 views
1 answer 299 views
1 answer 374 views
...