ifstream(ifstream读取文件)

更新时间:2023-03-01 05:43:52 阅读: 评论:0

C++中,ifstream和ofstream定义文件流的区别

区别一:本质不一样

1、ofstream是从内存到硬盘;

2、ifstream是从硬盘到内存

区别二:实际应用不同

1、ifstream默认以输入方式打开文件

2、ofstream默认以输出方式打开文件

扩展资料

1、C++对文件的输入输出操作需要用ifstream、ofstream和fstream类。

2、ifstream类支持文件的输入,ofstream类支持文件的输出操作,fstream类支持文件的输入输出操作,它们的定义在头文件<fstream>中。

3、C++将字符串也理解为一种输入输出设备,因此,也可以向终端设备和文件那样将数据输入输出到字符串中。

c++中输出和输入导屏幕和键盘的类别声明包含再标题文件<iostrream.h>中,而磁盘类文件的 I/O则声明再包含标题文件<fstream.h>内。

输入和输出格式:

输出到磁盘 ofsteam 识别字(“文件名”)

从磁盘读文件 ifsteam 识别字("文件名“)

例如:

ofstream outfile("data.txt"); //写入到磁盘的data.txt中


在头文件中使用ifstream时提示未声明的标识符,已包含相应文件

ifstream 是在 fstream 头文件中,所以要包含 fstream 头文件,并且 引用 std 命令空间。

在文件头部添加下面两句:

#include<fstream>
usingnamespacestd;

c++中,ifstream怎么实现拷贝复制?

使用C++标准程序库的输入输出流(I/OStream)复制文件,存在许多的方法,

方法一:逐个字符复制
#include<fstream>

std::ifstreaminput("in",ios::binary);
std::ofstreamoutput("out",ios::binary);
charch;

while(input.get(ch))output<<ch;

注意:如果使用input>>ch读取字符,则必须先调用input.untf(ios::skipws)取消输入流默认的跳过空白符的输入格式,因为换行符是空白符的一种。

方法二:逐行复制

#include<fstream>
#include<string>

std::ifstreaminput("in",ios::binary);
std::ofstreamoutput("out",ios::binary);
std::stringline;

while(getline(input,line))output<<line<<" ";

注意:这里的代码有一个小小的缺陷,如果文件不是纯文本格式的文件,或者文本文件的最后没有换行符,那么会导致复制后的文件末尾添加了一个多余的换行符。

方法三:迭代器复制

#include<fstream>
#include<iterator>
#include<algorithm>

std::ifstreaminput("in",ios::binary);
std::ofstreamoutput("out",ios::binary);
input.untf(ios::skipws);

copy(istream_iterator(input),istream_iterator(),ostream_iterator(output,""));

同样这里也有一个小技巧,输入流的格式默认为跳过空白字符,因此调用untf取消这个格式,才可保证正确的复制。

方法四:缓冲区复制

#include<fstream>

std::ifstreaminput("in",ios::binary);
std::ofstreamoutput("out",ios::binary);

output<<input.rdbuf();

这里直接使用了输入流的缓冲区,因此没有引入额外的临时对象。

很显然,上述四种方法中,最后一种方法最简洁,由于直接操作输入流的缓冲区,从运行效率上来说,也比其他方法有着略微的优势(当然,由于操作系统可能提供了额外的基于设备的文件缓冲机制,也许你无法证实这一点)。因此,除非要对输入内容进行处理,直接复制文件推荐最后一种方法,既不容易出错,又能获得良好的性能。
以上是搜索的资料,希望对你有帮助

c++ ifstream函数的使用

string 是stl里的模板,是为了方便字符串操作,当然不能传,可以用char数组,

#include<iostream>
#include<fstream>

using namespace std;

char a[50];

{
while(true)
{
cin >> a;
ifstream fin(a);
long temp;
fin >> temp;
fin.clo();
}
}
手机,没编译,有错请讲

怎么使用ifstream读取文件

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//输出空行
void OutPutAnEmptyLine()
{
cout<<"\n";
}

//读取方式: 逐词读取, 词之间用空格区分
//read data from the file, Word By Word
//when ud in this manner, we'll get space-delimited bits of text from the file
//but all of the whitespace that parated words (including newlines) was lost.
void ReadDataFromFileWBW()
{
ifstream fin("data.txt");
string s;
while( fin >> s )
{
cout << "Read from file: " << s << endl;
}
}

//读取方式: 逐行读取, 将行读入字符数组, 行之间用回车换行区分
//If we were interested in prerving whitespace,
//we could read the file in Line-By-Line using the I/O getline() function.
void ReadDataFromFileLBLIntoCharArray()
{
ifstream fin("data.txt");
const int LINE_LENGTH = 100;
char str[LINE_LENGTH];
while( fin.getline(str,LINE_LENGTH) )
{
cout << "Read from file: " << str << endl;
}
}

//读取方式: 逐行读取, 将行读入字符串, 行之间用回车换行区分
//If you want to avoid reading into character arrays,
//you can u the C++ string getline() function to read lines into strings
void ReadDataFromFileLBLIntoString()
{
ifstream fin("data.txt");
string s;
while( getline(fin,s) )
{
cout << "Read from file: " << s << endl;
}
}

//带错误检测的读取方式
//Simply evaluating an I/O object in a boolean context will return fal
//if any errors have occurred
void ReadDataWithErrChecking()
{
string filename = "dataFUNNY.txt";
ifstream fin( filename.c_str());
if( !fin )
{
cout << "Error opening " << filename << " for input" << endl;
exit(-1);
}
}

int main()
{
ReadDataFromFileWBW(); //逐词读入字符串
OutPutAnEmptyLine(); //输出空行

ReadDataFromFileLBLIntoCharArray(); //逐词读入字符数组
OutPutAnEmptyLine(); //输出空行

ReadDataFromFileLBLIntoString(); //逐词读入字符串
OutPutAnEmptyLine(); //输出空行

ReadDataWithErrChecking(); //带检测的读取
return 0;
}

本文发布于:2023-02-28 19:33:00,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/zhishi/a/167762063264698.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:ifstream(ifstream读取文件).doc

本文 PDF 下载地址:ifstream(ifstream读取文件).pdf

下一篇:返回列表
标签:文件   ifstream
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 实用文体写作网旗下知识大全大全栏目是一个全百科类宝库! 优秀范文|法律文书|专利查询|