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 137 views
1 answer 99 views
99 views asked Jan 29, 2023 by avibootz
1 answer 169 views
1 answer 166 views
166 views asked Jul 15, 2015 by avibootz
1 answer 203 views
1 answer 190 views
1 answer 262 views
...