c++对txt文件的读取与写入
转自:http://blog.csdn.net/lh3325251325/article/details/4761575
#include <iostream>
#include <iomanip>
#include <fstream> using namespace std; int main(){
char buffer[];
ifstream myfile ("c://a.txt");
ofstream outfile("c://b.txt"); if(!myfile){
cout << "Unable to open myfile";
exit(); // terminate with error }
if(!outfile){
cout << "Unable to open otfile";
exit(); // terminate with error }
int a,b;
int i=,j=;
int data[][];
while (! myfile.eof() )
{
myfile.getline (buffer,);
sscanf(buffer,"%d %d",&a,&b);
cout<<a<<" "<<b<<endl;
data[i][]=a;
data[i][]=b;
i++;
}
myfile.close();
for(int k=;k<i;k++){
outfile<<data[k][] <<" "<<data[k][]<<endl;
cout<<data[k][] <<" "<<data[k][]<<endl;
} outfile.close();
return ;
} 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dragonworrior/archive/2009/11/02/4759484.aspx
无论读写都要包含<fstream>头文件
读:从外部文件中将数据读到程序中来处理
对于程序来说,是从外部读入数据,因此定义输入流,即定义输入流对象:ifsteam infile,infile就是输入流对象。
这个对象当中存放即将从文件读入的数据流。假设有名字为myfile.txt的文件,存有两行数字数据,具体方法:
int a,b;
ifstream infile;
infile.open("myfile.txt"); //注意文件的路径
infile>>a>>b; //两行数据可以连续读出到变量里
infile.close() //如果是个很大的多行存储的文本型文件可以这么读:
char buf[]; //临时保存读取出来的文件内容
string message;
ifstream infile;
infile.open("myfile.js");
if(infile.is_open()) //文件打开成功,说明曾经写入过东西
{
while(infile.good() && !infile.eof())
{
memset(buf,,);
infile.getline(buf,);
message = buf;
...... //这里可能对message做一些操作
cout<<message<<endl;
}
infile.close();
}
写:将程序中处理后的数据写到文件当中
对程序来说是将数据写出去,即数据离开程序,因此定义输出流对象ofstream outfile,outfile就是输出流对象,这个对象用来存放将要写到文件当中的数据。具体做法:
ofstream outfile;
outfile.open("myfile.bat"); //myfile.bat是存放数据的文件名
if(outfile.is_open())
{
outfile<<message<<endl; //message是程序中处理的数据
outfile.close();
}
else
{
cout<<"不能打开文件!"<<endl;
}
c++对文件的读写操作的例子
/*/从键盘读入一行字符,把其中的字母依次放在磁盘文件fa2.dat中,再把它从磁盘文件读入程序,
将其中的小写字母改成大写字母,再存入磁盘fa3.dat中*/
#include<fstream>
#include<iostream>
#include<cmath>
using namespace std;
//////////////从键盘上读取字符的函数
void read_save(){
char c[];
ofstream outfile("f1.dat");//以输出方工打开文件
if(!outfile){
cerr<<"open error!"<<endl;//注意是用的是cerr
exit();
}
cin.getline(c,);//从键盘读入一行字符
for(int i=;c[i]!=;i++) //对字符一个一个的处理,直到遇到'/0'为止
if(c[i]>=&&c[i]<=||c[i]>=&&c[i]<=){//保证输入的字符是字符
outfile.put(c[i]);//将字母字符存入磁盘文件
cout<<c[i]<<"";
}
cout<<endl;
outfile.close();
}
void creat_data(){
char ch;
ifstream infile("f1.dat",ios::in);//以输入的方式打开文件
if(!infile){
cerr<<"open error!"<<endl;
exit();
}
ofstream outfile("f3.dat");//定义输出流f3.dat文件
if(!outfile){
cerr<<"open error!"<<endl;
exit();
}
while(infile.get(ch)){//当读取字符成功时
if(ch<=&&ch>=)
ch=ch-;
outfile.put(ch);
cout<<ch;
}
cout<<endl;
infile.close();
outfile.close();
}
int main(){
read_save();
creat_data();
system("pause");
return ;
}
c++对txt文件的读取与写入的更多相关文章
- c++学习笔记—c++对txt文件的读取与写入
一.文件的输入输出 头文件fstream定义了三个类型支持文件IO:ifstream从给定文件读取数据.ofstream向一个给定文件写入数据.fstream读写给定数据.这些类型与cin和cout的 ...
- c# txt文件的读取和写入
我们在工程实践中经常要处理传感器采集的数据,有时候要把这些数据记录下来,有时候也需要把记录下来的数据读取到项目中.接下来我们用C#演示如何对txt文件进行读写操作.我们要用到StreamReader ...
- C# txt文件的读取与写入
C#创建记事本方法一://创建对象 FileStream stream = new FileStream(@"d:\aa.txt",FileMode.Create);//fileM ...
- 《程序实现》从xml、txt文件里读取数据写入excel表格
直接上码 import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java ...
- (三)C#关于txt文件的读取和写入
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- c#中对txt文件的读取与写入,针对二维数组
class Program { ; ; static string[,] str = new string[ROW, COL]; static void Main(string[] args) { R ...
- 一些常用的文本文件格式(TXT,JSON,CSV)以及如何从这些文件中读取和写入数据
TXT文件: txt是微软在操作系统上附带的一种文本格式,文件以.txt为后缀. 从txt文件中读取数据: with open ('xxx.txt') as file: data=file.readl ...
- Java I/O---RandomAccessFile类(随机访问文件的读取和写入)
1.JDK API中RandomAccessFile类的描述 此类的实例支持对随机访问文件的读取和写入.随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组.存在指向该隐含数组的光标或索引 ...
- 编写Java程序,在硬盘中选取一个 txt 文件,读取该文档的内容后,追加一段文字“[ 来自新华社 ]”,保存到一个新的 txt 文件内
查看本章节 查看作业目录 需求说明: 在硬盘中选取一个 txt 文件,读取该文档的内容后,追加一段文字"[ 来自新华社 ]",保存到一个新的 txt 文件内 实现思路: 创建 Sa ...
随机推荐
- win10 设置C盘访问权限
“以管理员身份运行” cmd.exe C:\Windows\system32>icacls "C:\Program Files\Epic Games" /setintegri ...
- measure layout onMeasure() onLayout()
1.onMeasure() 在这个函数中,ViewGroup会接受childView的请求的大小,然后通过childView的 measure(newWidthMeasureSpec, heightM ...
- 工作表(Worksheet)基本操作应用示例
在编写代码时,经常要引用工作表的名字.知道工作表在工作簿中的位置.增加工作表.删除工作表.复制工作表.移动工作表.重命名工作表,等等.下面介绍与此有关及相关的一些属性和方法示例. [示例04-01]增 ...
- 在input文本框中存入对象格式的数据
<input id="teaching" type="hidden" name="teachingProgram" /> JQ: ...
- 构建API
前言 过程,如图: 第一步创建一个帮助类,类里面提供了加密.组装Url等方法,代码如下: using System; using System.Collections.Generic; using S ...
- centos6上安装docker
yum -y install epel-releaseyum -y install docker-ioyum install device-mapper-event-libs # 必需安装这一步,否 ...
- PacificA: Replication in Log-Based Distributed Storage Systems
PacificA: Replication in Log-Based Distributed Storage Systems - Microsoft Research https://www.micr ...
- 【推荐】CentOS安装Subversion-1.8.17+HTTP协议支持配置
注:以下所有操作均在CentOS 6.5 x86_64位系统下完成. 我们需要搭建一个自己的SVN服务器. 此外,搭建好的SVN服务器除了需要支持svn协议外,最好还需要支持HTTP协议和HTTPS协 ...
- 在django中实现支付宝支付(支付宝接口调用)
支付宝支付 正式环境:用营业执照,申请商户号,appid 测试环境:沙箱环境:https://openhome.alipay.com/platform/appDaily.htm?tab=info 支付 ...
- Laravel 5.3 使用内置的 Auth 组件实现多用户认证功能
https://blog.csdn.net/kevinbai_cn/article/details/54341779 概述 在开发中,我们经常会遇到多种类型的用户的认证问题,比如后台的管理员和前台的普 ...