C++文件操作

#include <fstream>  包含三个基本的类:fstream/ifstream/ofstream(输入输出文件、输入文件、输出文件)
1.   打开文件 open()
void open(const char* filename, int access);
参数:
filename   文件名,用双斜杠,例如 C:\\path\\123.txt
access       打开文件的属性。如下列表:
ios::app:   以追加的方式打开文件 
ios::ate:   文件打开后定位到文件尾,ios:app就包含有此属性 
ios::binary:  以 二进制方式打开文件,缺省的方式是文本方式。两种方式的区别见前文 

ios::in:    文件以输入方式打开 
ios::out:   文件以输出方式打开 
ios::nocreate: 不建立文件,所以文件不存在时打开失败  
ios::noreplace:不覆盖文件,所以打开文件时如果文件存在失败 
ios::trunc:  如果文件存在,把文件长度设为0 
  可以用“或”把以上属性连接起来,如ios::out|ios::binary

用与 把以上属性连接起来,如ios::out+ios::binary

  打开文件的属性取值是:

0:普通文件,打开访问 
1:只读文件 
2:隐含文件 
4:系统文件 
  可以用“或”或者“+”把以上属性连接起来 ,如3或1|2就是以只读和隐含属性打开文件。

2、两个重要的运算符 <<     >>
2.1   插入器 <<
2.2   析取器 >>

常用的错误判断方法:

good() 如果文件打开成功

bad() 打开文件时发生错误

eof() 到达文件尾

 

按特定字符读取文本
=====================
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
 ifstream op;
 op.open("123.txt", ios::in);
 if(op.fail())
 throw runtime_error("Open file error!!!");
 char buf[256];
 while(!op.eof())
 {
      op.getline(buf,256,'\n'); //按行读取
    op.getline(buf,256,' ');//按空格读取
    op.getline(buf,256,'a');//遇到a则重新开始读取下一个字符串
    
  cout<<buf<<endl;
 }
 op.close();
 return 0;
}

按照单词读取字符串
======================================
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
 ifstream op;
 ifstream ifs("123.txt");
 string str;
 while(ifs>>str)
 {
   cout<<str<<endl;
 }
 
 return 0;
}


 

写文件

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream in;
in.open("123.txt", ios::trunc);
int i;
char a = 'a';
for(i = 1; i <= 26; i ++) 
{
if(i < 10)
in<<"0"<<i<<"\t"<<a<<endl;
else
in<<i<<"\t"<<a<<endl;
a ++;
}
in.close();
return 0;
}


读文件操作:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream op;
op.open("123.txt", ios::in);
if(op.fail())
throw runtime_error("Open file error!!!");
char buf[256];
while(!op.eof())
{
op.getline(buf,256,'\n');
cout<<buf<<endl;
}
op.close();
return 0;
}


 

// Cluster.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{	

	/*fstream op;
	op.open("E:\\123.txt", ios::in);
	if(op.fail())
	throw runtime_error("Open file error!!!");
	char buf[256];
	while(!op.eof())
	{
	op.getline(buf,256,'\n');
	cout<<buf<<endl;
	}
	op.close();*/

	/*int count = 0;
    FILE* fp;
    char str[100];

    fp = fopen("E:\\123.txt", "r");
    while (fscanf(fp, "%s", str) != EOF)
    {
        printf("%s\n", str);
        count++;
    }
    fclose(fp);*/
	ifstream ifs("E:\\123.txt");
    string str;
    int count = 0;

    while (ifs >>str)
    {
        cout << str << endl;
        count++;
    }
    ifs.close();


	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值