【c++】文件操作

来源:互联网 发布:锐捷客户端mac 编辑:程序博客网 时间:2024/06/15 19:34

先看一个C语言文件复制程序

#include<stdio.h>#include<stdlib.h>int main(int argc,char* argv[] ){FILE *in,*out;int ch;if(argc != 3){fprintf( stderr, "输入形式: copyFile 源文件名 目标文件名 \n");exit( EXIT_FAILURE );}if((in=fopen( argv[1],"rb"))==NULL){fprintf( stderr, "打不开文件:%s \n", argv[1] );exit( EXIT_FAILURE );}if((in=fopen( argv[2],"wb"))==NULL){fprintf( stderr, "打不开文件:%s \n", argv[2] );fclose( in );exit( EXIT_FAILURE );}while( (ch = getc(in)) != EOF ){if( putc( ch, out )== EOF )break;}if( ferror(in) )printf("读取文件 %s 失败! \n", argv[1]);if( ferror(out) )printf("写入文件 %s 失败! \n", argv[2]);fclose(in);fclose(out);return 0;}
//这个程序是从命令行运行的,我试了运行不了,姿势不对,改天再学习
argc与argv[]

在程序中,main函数有两个参数,整型变量argc和字符指针数组argv【】

Argc的含义是程序的参数数量,包含本身。

Argv[ ]的每个指针指向命令行的一个字符串,所以argv【0】指向字符串”copyFile.exe”.

Argv[1]指向字符串sourceFile,argv【2】指向字符串destFile.

Getc()函数一次从输入流(stdin)读取一个字符,putc()函数把这个字符写入到输出流(stdout)。

当getc()遇到文件结束标志的时候,函数就返回EOF。EOF是一个宏,在stdio.h中定义,

EOF事实上有两个含义。

注意细节,getc()的返回值是int型,所以我们声明是应该是int

ch,而不是char ch


C++文件I/O

【例一】

#include "iostream"#include "fstream"using namespace std; int main(){        /*ifstream  in;    //读取文件类         in.open("test.txt");         if(!in)         {              cout<<"打开文件失败"<<endl;              return 0;         }         char x;         while(in>>x)         {              cout<<x;         }         cout <<endl;         in.close();          return 0;*/          ofstream out;   //写入文件类         out.open("test.txt");         if(!out)         {              cerr<<"打开文件失败!"<<endl;              return 0;         }          for(int i = 0;i<10;i++)         {              out<<i;         }         cout<<endl;         out.close();         return 0;}


【例二】

#include "iostream"#include "fstream"#include "string"#include "windows.h"using namespace std;int main(void){cout<<"正在写入数据.....";for(int x = 1;x<=100;x++){cout.width(3);cout<<x<<"%";Sleep(25);  //执行挂起一段时间,毫秒为单位,windows.h中定义cout<<"\b\b\b\b";//退格}ofstream source;source.open("test.txt",ios::trunc);//ios::trunc 删除文件原来存在的内容int i;char a = 'a';for(i = 1;i<=16;i++){if(i<10){source<<"0"<<i<<"\t"<<a<<"\n";a++;}else{source<<i<<"\t"<<a<<"\n";a++;}}source.close();cout<<"\n\n写入数据成功^_^.....\n";return 0;}

前面的例子中出现

ifstream in;

in.open(”test.txt”);

ofstream out;

out.open(”test.txt”);

其实我们可以用ifstream in(”test.txt”);   和   ofstream out(”test.txt”);代替

代码在创建一个ifstream和ofstream类的对象,将文件名字传递给构造函数。

Ifstream in(char* filename,int open_mode)

filename表示文件的名称,是一个字符串

open_mode表示打开模式,其值用来定义以怎样的方式打开文件


【常见的文件打开方式】

ios::in 打开一个可读取文件

ios::out 打开一个可写入文件

ios::binary 以二进制的形式打开一个文件

ios::app 写入的所有数据将被追加到文件的末尾

ios::trunk 删除文件原来已存在的内容

ios::nocreate 如果要打开的文件不存在,那么用此参数调用open函数将无法运行

ios::noreplace 如果打开的文件已存在,试图用open函数打开是将返回一个错误


//文件复制程序
#include "iostream"#include "fstream"using namespace std;int main(void){ifstream source("test.txt");//读取文本文件ofstream destination("1.txt",ios::app);//创建文本文件char ch;while(source.get(ch))//读取文本中的内容{cout<<ch;destination<<ch;//写入内容到文件}source.close();destination.close();cout<<"\n\n复制成功^_^.....\n"<<endl;return 0;}










原创粉丝点击