1 / 5

Chapter 8

Chapter 8. Files: Electronic Storage. How to work with files. #include <fstream> To write to a file: ofstream fout("output.txt"); fout << "Hi There"; fout.close(); To read from a file: ifstream fin("input.txt"); fin >> s; // or fin.getline(s,80) to get complete line fin.close();.

althea
Download Presentation

Chapter 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 8 Files: Electronic Storage

  2. How to work with files • #include <fstream> • To write to a file: • ofstream fout("output.txt"); • fout << "Hi There"; • fout.close(); • To read from a file: • ifstream fin("input.txt"); • fin >> s; // or fin.getline(s,80) to get complete line • fin.close();

  3. void main(){ char s[100]; ofstream fout("data.txt"); if(fout){ fout << "Hi There!"; fout.close(); } ifstream fin("data.txt"); if(fin){ fin >> s; cout << s; fin.close(); } }

  4. How to refer to disk files • c:\\users\\lubo\data.txt • \\ because \ is a special character

  5. Reading all the words from a file int main(){ char s[100]; ifstream fin("data.txt"); if(fin){ while(!fin.eof()){ fin >> s; cout << s; } fin.close(); } }

More Related