[C++]C++小笔记之Simple File read and write-- ofstream and ifstream

快来打我* 2022-09-19 15:28 259阅读 0赞

首先我们简单的复习一下cin.

  1. int n;
  2. cin >> n;

if user input a word instead of a number, four things will happen:

  1. the value of n remain unchanged;
  2. mismatch input is left in input queue;
  3. An error flag is set to the cin object;
  4. the call to the cin method, if converted to type bool, returns false. if ( cin >> n) returns false;

在cin 读取出错的情况下,因为被设了error flag, 所以在继续读取更多的input之前,需要使用 cin.clear()方法reset flag. clear()方法不仅会reset error flag; 它还可以reset EOF condition.

ofstream — Simple File Write To a txt File.

与cout用法相同,当要写内容进入文件时, ofstream objects可以使用所有cout同名的函数。

  • must include a fstream header file;
  • fstream header file defines an ofstream class for handling output.
  • name one or more ofstream objects;
  • using namespace std OR use std:: prefix;
  • associate ofstream object with a specific file. One way to do so is to use open(“test.txt”) method;
  • use close() method to close the file when finished write the file.
  • use ofstream object with << operator to outpu a variety of data types;

Create a file:

  1. fout.open("test.txt"); // if no such file, it will be created; if have file, clear its contents.
  2. OR
  3. char filename[50];
  4. cin >> filename; //enter the filename that wants to write
  5. fout.open(filename);
  6. double wt=125.8;
  7. fout<<wt; //write a number to txt file
  8. char line[4] = "xyz";
  9. fout<< line <<endl;
  10. fout.close();
  11. char automobile[50];
  12. int year;
  13. double a_price, d_price;
  14. ofstream outFile;
  15. outFile.open("carinfo.txt");
  16. cout << "Enter the make and model of the car: ";
  17. cin.getline(automobile, 50);
  18. cout << "Enter the model year: ";
  19. cin >> year;
  20. cout << "Enter the original asking price: ";
  21. cin >> a_price;
  22. d_price = 0.913 * a_price;
  23. cout << fixed;
  24. cout.precision(2);
  25. cout.setf(ios_base::showpoint);
  26. cout << "make and model: " << automobile << endl;
  27. cout << "year: " << year << endl;
  28. cout << "price: " << d_price << endl;
  29. outFile << fixed;
  30. outFile.precision(2);
  31. outFile.setf(ios_base::showpoint);
  32. outFile << "make and model: " << automobile << endl;
  33. outFile << "year: " << year << endl;
  34. outFile << "price: " << d_price << endl;
  35. outFile.close();

ifstream — Simple File Read From a txt file.

与cin用法相同,当要写内容进入文件时, ifstream objects可以使用所有cin同名的函数。

  • must include a fstream header file;
  • fstream header file defines an ifstream class for handling input;
  • name one or more ifstream objects;
  • if stream use >> operator to read a variaty of data types;
  • ifstream object use get() function to read a individual character and use getline() fucntion to read a line of characters.
  • ifstream object use eof(), fail() functions to monitor for the success of an input attempt;
  • ifstream object itself, when used as a test condition is converted to boolean value true if last read attemp scceded and to false if otherwise.
  1. char filename[50];
  2. ifstream readFile;
  3. cout << "Enter the name of the data file: ";
  4. cin.getline(filename, 50);
  5. readFile.open(filename);
  6. if (!readFile.is_open()) {
  7. cout << "Could not open file " << filename << endl;
  8. cout << "Program terminating.\n";
  9. return false;
  10. }
  11. char content[100];
  12. readFile >> content;
  13. while (readFile.good()) {
  14. cout << content;
  15. readFile>>content;
  16. }
  17. if (readFile.eof()) {
  18. cout << " Reached at the end of file." << endl;
  19. } else if (readFile.fail()) {
  20. cout<<"Input terminated by data mismatch" <<endl;
  21. } else {
  22. cout<<"Input terminated for unknown reason"<<endl;
  23. }

is_open(“test.txt”) function shows if a file can be opened succesfully.

fail() function returns true if read EOF or a type mis-match. e.g.

  1. readFile >> content;

bad() function returns false when something unexpected may go wrong.

发表评论

表情:
评论列表 (有 0 条评论,259人围观)

还没有评论,来说两句吧...

相关阅读