C++ Tutorial/File Stream/Text file write
Содержание
- 1 file output with characters
- 2 file output with strings
- 3 Output a text file character by character
- 4 Output string and decimal in hexadecimal format to a file with ostrstream
- 5 Output to file line by line
- 6 Save string double pair to file
- 7 Use ofstream to write text to a file
- 8 Use put() to write to a file
- 9 Write formatted output to a text file.
- 10 Write to file
file output with characters
<source lang="cpp">#include <fstream>
#include <iostream> #include <string> using namespace std; int main(){ string str = "AAA"; ofstream outfile("TEST.TXT"); for(int j=0; j<str.size(); j++) outfile.put( str[j] ); cout << "File written\n"; return 0; }</source>
file output with strings
<source lang="cpp">#include <fstream>
using namespace std; int main(){ ofstream outfile("TEST.TXT"); outfile << "test\n"; outfile << "1\n"; outfile << "2\n"; outfile << "3\n"; return 0; }</source>
Output a text file character by character
<source lang="cpp">#include <iostream>
- include <fstream>
using namespace std; int main(void){
ifstream in("test.cpp"); if(!in){ cout << "Cannot open file." << endl; exit (1); } in.ignore(10, " "); char c; while(in) { in.get(c); cout << c; } in.close();
}</source>
Output string and decimal in hexadecimal format to a file with ostrstream
<source lang="cpp">#include <strstream>
- include <iostream>
using namespace std; main() {
char str[80]; int a = 10; ostrstream outs(str, sizeof(str)); outs << "Hello there "; outs << a+44 << hex << " "; outs.setf(ios::showbase); outs << 100 << ends; cout << str; return 0;
}</source>
Output to file line by line
<source lang="cpp">#include <fstream.H>
int main () {
ofstream myfile("test.txt"); if (myfile.is_open()) { myfile << "This outputting a line.\n"; myfile << "this is another line.\n"; myfile.close(); } return 0;
}</source>
Save string double pair to file
<source lang="cpp">#include <iostream>
- include <fstream>
using namespace std; int main() {
ofstream out("test.txt"); // output, normal file if(!out) { cout << "Cannot open test.txt file.\n"; return 1; } out << "R " << 9.9 << endl; out << "T " << 9.9 << endl; out << "M " << 4.8 << endl; out.close(); return 0;
}</source>
Use ofstream to write text to a file
<source lang="cpp">#include <iostream>
- include <fstream>
using namespace std; main(void) {
char *p = "hello there"; ofstream out("test"); if(!out) { cout << "Cannot open file"; return 1; } while(*p) out.put(*p++); out.close(); return 0;
}</source>
Use put() to write to a file
<source lang="cpp">#include <iostream>
- include <fstream>
using namespace std;
int main() {
char *p = "hello\n"; ofstream out("test", ios::out | ios::binary); if(!out) { cout << "Cannot open file.\n"; return 1; } // Write characters until the null-terminator is reached. while(*p) out.put(*p++); out.close(); return 0;
}</source>
Write formatted output to a text file.
<source lang="cpp">#include <iostream>
- include <fstream>
using namespace std; int main() {
ofstream fout("test.dat"); if(!fout) { cout << "Cannot open file.\n"; return 1; } fout << 10 << " " << -20 << " " << 30.2 << "\n"; fout << "This is a test."; fout.close(); if(!fout.good()) { cout << "A file error occurred."; return 1; }
}</source>
Write to file
<source lang="cpp">#include <iostream>
- include <fstream>
using namespace std;
int main() {
ofstream out("test.txt"); if(!out) { cout << "Cannot open file.\n"; return 1; } out << 10 << " " << 123.23 << "\n"; out << "This is a text."; out.close(); return 0;
}</source>