• 170 likes • 311 Views
File Stream Работа со датотеки. вежби 9. fstream. Open a file for output then write to that file #include <fstream> #include <iostream> using namespace std; int main () { char buffer[256]; // open it for output then write to it fstream myfile;
E N D
File StreamРабота со датотеки вежби 9
fstream Open a file for output then write to that file #include <fstream> #include <iostream> using namespace std; int main () { char buffer[256]; // open it for output then write to it fstream myfile; myfile.open("test.txt",ios::out | ios::trunc); if (myfile.is_open()) { myfile << "This outputting a line.\n"; myfile.close(); } system("pause"); return 0; }
Write to file #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(); system("pause"); return 0; }
Save string double pair to file #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(); system("pause"); return 0; }
Use put() to write to a file #include <iostream> #include <fstream> using namespace std; int main() { char *p = "hello\n"; ofstream out("test.txt", 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(); system("pause"); return 0; }
Output to file line by line #include <fstream.H> int main () { ofstream myfile("test.txt"); if (myfile.is_open()) { myfile << "This outputting a line.“<<endl; myfile << "this is another line.“<<endl; myfile.close(); } system("pause"); return 0; }
Read string and float value from a file #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(); ifstream in("test.txt"); // input if(!in) { cout << "Cannot open test.txt file.\n"; return 1; } char item[20]; float cost; in >> item >> cost; cout << item << " " << cost << "\n"; in >> item >> cost; cout << item << " " << cost << "\n"; in >> item >> cost; cout << item << " " << cost << "\n"; in.close(); system("pause"); return 0; }
Read from file #include <iostream> #include <fstream> using namespace std; int main() { char ch; inti; float f; char str[80]; 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(); ifstream in("test.txt"); if(!in) { cout << "Cannot open file.\n"; return 1; } in >> i; in >> f; in >> ch; in >> str; cout << i << " " << f << " " << ch << "\n"; cout << str; in.close(); system("pause"); return 0; }
Read a text file line by line #include <fstream> #include <iostream> using namespace std; int main () { char buffer[256]; ifstreammyfile ("test.txt"); while (! myfile.eof() ) { myfile.getline (buffer,100); cout << buffer << endl; } system("pause"); return 0; }
Open a file for input and read in its content #include <fstream> #include <iostream> using namespace std; int main () { char buffer[256]; fstream myfile; // open it for input and read in myfile.open("test.txt",ios::in); myfile.getline(buffer,100); cout << "The file contains " << buffer << "\n"; myfile.close(); system("pause"); return 0; }
Open a file for appending and append(додавање) #include <fstream>#include <iostream>using namespace std;int main () { char buffer[256];fstreammyfile; //open for appending and appendmyfile.open("test.txt",ios::app);myfile << " Hey this is another line \n";myfile.close(); return 0;}
Use ifstream and ofstream to copy file #include <iostream> #include <fstream> using namespace std; main(void) { char ch; ifstream f1("test.txt"); ofstream f2("text1.txt"); if(!f1) cerr <<"Can't open IN file\n"; if(!f2) cerr << "Can't open OUT file\n"; while(f1 && f1.get(ch) ) f2.put(ch); system("pause"); return 0; }
Check file status #include <iostream> #include <fstream> using namespace std; void checkstatus(ifstream &in); int main(intargc, char *argv[]) { ifstream in("test.txt"); char c; while(in.get(c)) { checkstatus(in); } checkstatus(in); // check final status in.close(); system("pause"); return 0; } void checkstatus(ifstream &in) { ios::iostatei; i = in.rdstate(); if(i & ios::eofbit) cout << "EOF encountered\n"; else if(i & ios::failbit) cout << "Non-Fatal I/O error\n"; else if(i & ios::badbit) cout << "Fatal I/O error\n"; }
IfstreamRead and display a text file line by line. #include <iostream> #include <fstream> using namespace std; int main() { ifstream in("test.txt"); if(!in) { cout << "Cannot open input file."<<endl; return 1; } char str[255]; while(in) { in.getline(str, 255); // delim defaults to '\n' if(in) cout << str << endl; } in.close(); system("pause"); return 0; }
Display a file using ifstream.get() #include <iostream> #include <fstream> using namespace std; int main() { char ch; ifstream in("test", ios::in | ios::binary); if(!in) { cout << "Cannot open file.\n"; return 1; } while(in) { // in will be false when eof is reached in.get(ch); if(in) cout << ch; } in.close(); system("pause"); return 0; }
Use ifstream.read() and ofstream.write() #include <iostream> #include <fstream> using namespace std; int main() { int n[5] = {1, 2, 3, 4, 5}; register inti; ofstream out("test", ios::out | ios::binary); if(!out) { cout << "Cannot open file.\n"; return 1; } out.write((char *) &n, sizeof n); out.close(); for(i=0; i<5; i++) // clear array n[i] = 0; ifstream in("test", ios::in | ios::binary); if(!in) { cout << "Cannot open file.\n"; return 1; } in.read((char *) &n, sizeof n); for(i=0; i<5; i++) // show values read from file cout << n[i] << " "; in.close(); system("pause"); return 0; }
Reading a text file #include <iostream> #include <fstream> using namespace std; int main () { char buffer[256]; ifstreamexamplefile ("test.txt"); if (! examplefile.is_open()) { cout << "Error opening file"; exit (1); } while (! examplefile.eof() ) { examplefile.getline (buffer,100); cout << buffer << endl; } system("pause"); return 0; }