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 149 views
1 answer 111 views
111 views asked Jan 29, 2023 by avibootz
1 answer 188 views
1 answer 184 views
184 views asked Jul 15, 2015 by avibootz
1 answer 215 views
1 answer 203 views
1 answer 275 views
...