天津大学《C语言程序设计》课件-第12章.pptVIP

  • 0
  • 0
  • 约9.48千字
  • 约 36页
  • 2023-01-08 发布于贵州
  • 举报

天津大学《C语言程序设计》课件-第12章.ppt

File Streams and File I/OChapter 12 Now a deeper look at StreamsWhat is a stream (in real life)? Now a deeper look at StreamsWhat is a stream (in real life)?a flow of water from one place to another Now a deeper look at StreamsWhat is a stream (in real life)?a flow of water from one place to anotheriostreams are the same concept, but are a flow of data from one point to another Now a deeper look at StreamsWhat is a stream (in real life)?a flow of water from one place to anotheriostreams are the same concept, but are a flow of data from one point to anotheristream: bring information in (cin)ostream: take information out (cout)ifstream: bring information in from a fileofstream take information out to a file ifstream will have the same operations as istream ofstream will have the same operations as ostream File Streams: fstreamSo what do we want to do with filesopen them for reading or writingread or write to themcheck if we have finished reading close them after using them Opening a File Create a variable of ifstream or ofstreamUse the open functionifstream fin; // create the ifstream variablefin.open(“myfile.txt”); // tries to open the fileint i;fin i; // reading from the fileNote: myfile.txt will need to be in the same directory as where the program is running. Opening a File Opening a ifstream variable tries to open an existing file read only modeOpening an ofstream variable opensa new file with the file name given by your programwrite only modeofstream fout; // create ofstream variablefout.open(“myfile.txt”); // tries to create the file Checking to see if a file opened Two ways to check to see if a file is read for use:fail( ) function – returns true if not ready to readif ( fin == false ) – fstream variable will be equal to false if not readyifstream fin; // create the ifstream variablefin.open(“myfile.txt”); // tries to open the fileif ( fin.fail( ) ) { // check if file is ready // do something} What can you do

文档评论(0)

1亿VIP精品文档

相关文档