c++文件流操作
2016-04-18 12:40
274 查看
#include <iostream>
#include <fstream> //(1)包含头文件<fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
ifstream ifile; //(2)建立文件流对象
ofstream ofile;
ifile.open("d:\\fileIn.txt"); //(3)打开D盘根目录下的fileIn.txt文件
ofile.open("d:\\fileOut.txt");
if(ifile.fail() || ofile.fail()) { //测试打开操作是否成功
cerr<<"open file fail"<<endl;
return EXIT_FAILURE; //返回 EXIT_FAILURE(在cstdlib库中定义)
//用于向操作系统报告终止不成功
}
char ch;
ch = ifile.get(); //(4) 进行读写操作
while(!ifile.eof()) {
ofile.put(ch);
ch = ifile.get();
}
ifile.close(); //(5) 关闭文件
ofile.close();
return 0;
}
#include <fstream> //(1)包含头文件<fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
ifstream ifile; //(2)建立文件流对象
ofstream ofile;
ifile.open("d:\\fileIn.txt"); //(3)打开D盘根目录下的fileIn.txt文件
ofile.open("d:\\fileOut.txt");
if(ifile.fail() || ofile.fail()) { //测试打开操作是否成功
cerr<<"open file fail"<<endl;
return EXIT_FAILURE; //返回 EXIT_FAILURE(在cstdlib库中定义)
//用于向操作系统报告终止不成功
}
char ch;
ch = ifile.get(); //(4) 进行读写操作
while(!ifile.eof()) {
ofile.put(ch);
ch = ifile.get();
}
ifile.close(); //(5) 关闭文件
ofile.close();
return 0;
}
相关文章推荐
- C++中Static作用和使用方法
- c++类的拷贝构造函数、隐式转换、重载的赋值操作符的一些分析和实例
- [C++]关于STL慎重选择删除元素的方法
- 友元类
- leetcode_226_Invert Binary Tree(C++)(easy)
- Java内联函数(华为面试)-java可能比c++更快
- c语言随机数
- [C++11]新特性——引入了nullptr
- 在C++中子类继承和调用父类的构造函数方法
- 在vc++工程中只使用tlb文件的用法
- C++空类的sizeof
- C语言sscanf函数的总结
- 设计模式中的构建者Builder模式&c++
- C++11新特性之智能指针
- cl.exe命令方式编译cpp
- 文件中有一组整数,要求排序后输出到另一个文件中
- leetcode_104._Maximum Depth of Binary Tree(C++)(easy)
- c/c++创建动态链接库
- python中的赋值操作,与c++做对比
- C语言结构体的字节对齐