[C++]C++小笔记之Simple File read and write-- ofstream and ifstream
来源:互联网 发布:在线授课软件 编辑:程序博客网 时间:2024/06/07 03:02
首先我们简单的复习一下cin.
int n;cin >> n;
if user input a word instead of a number, four things will happen:
- the value of n remain unchanged;
- mismatch input is left in input queue;
- An error flag is set to the cin object;
- the call to the cin method, if converted to type bool, returns false. if ( cin >> n) returns false;
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:
fout.open("test.txt"); // if no such file, it will be created; if have file, clear its contents.ORchar filename[50];cin >> filename; //enter the filename that wants to writefout.open(filename);double wt=125.8;fout<<wt; //write a number to txt filechar line[4] = "xyz";fout<< line <<endl;fout.close();
char automobile[50];int year;double a_price, d_price;ofstream outFile;outFile.open("carinfo.txt");cout << "Enter the make and model of the car: ";cin.getline(automobile, 50);cout << "Enter the model year: ";cin >> year;cout << "Enter the original asking price: ";cin >> a_price;d_price = 0.913 * a_price;cout << fixed;cout.precision(2);cout.setf(ios_base::showpoint);cout << "make and model: " << automobile << endl;cout << "year: " << year << endl;cout << "price: " << d_price << endl;outFile << fixed;outFile.precision(2);outFile.setf(ios_base::showpoint);outFile << "make and model: " << automobile << endl;outFile << "year: " << year << endl;outFile << "price: " << d_price << endl;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.
char filename[50];ifstream readFile;cout << "Enter the name of the data file: ";cin.getline(filename, 50);readFile.open(filename);if (!readFile.is_open()) {cout << "Could not open file " << filename << endl;cout << "Program terminating.\n";return false;}char content[100];readFile >> content;while (readFile.good()) {cout << content;readFile>>content;}if (readFile.eof()) {cout << " Reached at the end of file." << endl;} else if (readFile.fail()) {cout<<"Input terminated by data mismatch" <<endl;} else {cout<<"Input terminated for unknown reason"<<endl;}
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.
readFile >> content;
bad() function returns false when something unexpected may go wrong.
0 0
- [C++]C++小笔记之Simple File read and write-- ofstream and ifstream
- Something about file's read and write (C++)
- file read and write
- ifstream and ofstream
- Read and Write File Code
- read and write '.bmp' file
- c write in file and '\n'
- 【c++】ofstream和ifstream
- QT之write and read
- JSON.simple example – Read and write JSON Read
- [zz] Read and write a properties file
- file read and write use java
- Read and Write a /proc File
- Example : Linux kernal file read and write
- read from and write to file
- Ubuntu Read and Write NetCDF file
- python.pandas read and write CSV file
- 四种读写文件的方式:系统调用(open/read/write),C语言(fopen,fgets, fputs),C++(ifstream, ofstream getline,)泛型算法
- 高通量测序与杂交优势
- 揭秘百度蜘蛛的七大特性!
- 黑马程序员——学习笔记02.java的语言基础
- Android性能调优工具TraceView介绍
- 编译静态库、动态库以及条件编译用法
- [C++]C++小笔记之Simple File read and write-- ofstream and ifstream
- [LeetCode]Generate Parentheses
- [易飞]9.0新增账套
- libcurl的使用 ftp篇(easy模式)
- 做百度优化常见的十个问题及解答
- LabelAtlas自定义字体
- Flash AS3.0关于TextField的过滤,HTML格式,对齐,换行等介绍
- PreloadDataCache
- struts2文件下载出现Can not find a java.io.InputStream with the name的错误