file stream Работа со датотеки

17
File Stream Работа со датотеки вежби 9

Upload: stacey-burgess

Post on 15-Mar-2016

66 views

Category:

Documents


2 download

DESCRIPTION

File Stream Работа со датотеки. вежби 9. fstream. Open a file for output then write to that file #include #include using namespace std; int main () { char buffer[256]; // open it for output then write to it fstream myfile; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: File Stream Работа со датотеки

File StreamРабота со датотеки

вежби 9

Page 2: File Stream Работа со датотеки

fstreamOpen 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;}

Page 3: File Stream Работа со датотеки

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; }

Page 4: File Stream Работа со датотеки

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;}

Page 5: File Stream Работа со датотеки

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; }

Page 6: File Stream Работа со датотеки

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;}

Page 7: File Stream Работа со датотеки

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;}

Page 8: File Stream Работа со датотеки

Read from file#include <iostream> #include <fstream> using namespace std; int main() { char ch; int i; 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; }

Page 9: File Stream Работа со датотеки

Read a text file line by line#include <fstream>#include <iostream>using namespace std;int main () { char buffer[256]; ifstream myfile ("test.txt");

while (! myfile.eof() ) { myfile.getline (buffer,100); cout << buffer << endl; } system("pause"); return 0;}

Page 10: File Stream Работа со датотеки

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;}

Page 11: File Stream Работа со датотеки

Open a file for appending and append(додавање)

#include <fstream>#include <iostream>using namespace std;   int main () {  char buffer[256];   fstream myfile;  //open for appending and append  myfile.open("test.txt",ios::app);  myfile << " Hey this is another line \n";  myfile.close();   return 0;}

Page 12: File Stream Работа со датотеки

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;}

Page 13: File Stream Работа со датотеки

Check file status#include <iostream>#include <fstream>using namespace std;

void checkstatus(ifstream &in);

int main(int argc, 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::iostate i;

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";}

Page 14: File Stream Работа со датотеки

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;}

Page 15: File Stream Работа со датотеки

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; }

Page 16: File Stream Работа со датотеки

Use ifstream.read() and ofstream.write()

#include <iostream> #include <fstream> using namespace std; int main() { int n[5] = {1, 2, 3, 4, 5}; register int i; 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; }

Page 17: File Stream Работа со датотеки

Reading a text file#include <iostream>#include <fstream>using namespace std;int main () { char buffer[256]; ifstream examplefile ("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;}