ifstream源代码(ifstream file)[20240427更新]
admin 发布:2024-04-27 15:43 125
本篇文章给大家谈谈ifstream源代码,以及ifstream file对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、C++程序关于ifstream和ofstream。求怎么做
- 2、C++关于的ifstream和ofstream的一点小问题
- 3、c++中,ifstream怎么实现拷贝复制?
- 4、c++从txt文件读入数据到程序中出现问题,源代码如下:
- 5、c++ MFC ifstream 问题
C++程序关于ifstream和ofstream。求怎么做
#include iostream
#include fstream
#include string
using namespace std;
void count_letters(ifstream, int*);
void output_letters(ofstream, int*);
int main()
{
int counts[26] = {0};
string fname;
ifstream ifs;
while (true) {
cout "Input file name: ";
cin fname;
ifs.clear();
ifs.open(fname.c_str());
if (!ifs.fail())
break;
cerr "Can't open file: " fname "\n";
}
count_letters(ifs, counts);
ofstream ofs;
while (true) {
cout "Input output file name: ";
cin fname;
ofs.clear();
ofs.open(fname.c_str());
if (!ofs.fail())
break;
cerr "Can't create file: " fname "\n";
}
output_letters(ofs, counts);
}
void output_letters(ofstream ofs, int* counts)
{
for (int i = 0; i 26; i++)
ofs char('a'+i) ": " counts[i] "\n";
}
void count_letters(ifstream ifs, int* counts)
{
char c;
while (true) {
ifs c;
if (ifs.fail() || ifs.eof())
break;
if (c = 'a' c = 'z')
counts[c-'a']++;
if (c = 'A' c = 'Z')
counts[c+32-'a']++;
}
}
源程序在 g++ 4.5 已测试。
测试文件 a.txt:
aA aA bB B
C d Z z 232
输出文件 o.txt:
a: 4
b: 3
c: 1
d: 1
e: 0
f: 0
g: 0
h: 0
i: 0
j: 0
k: 0
l: 0
m: 0
n: 0
o: 0
p: 0
q: 0
r: 0
s: 0
t: 0
u: 0
v: 0
w: 0
x: 0
y: 0
z: 2
C++关于的ifstream和ofstream的一点小问题
outfilestring2;输出后字符串还在缓冲区,并没有真正地写到磁盘文件。有两种方法解决这个问题:
outfilestring2flush;//~ 输出后刷新缓冲区
或者在输出后关闭文件(关闭文件时会自动刷新缓冲区)
outfilestring2;
outfile.close();
c++中,ifstream怎么实现拷贝复制?
使用C++标准程序库的输入输出流(I/O Stream)复制文件,存在许多的方法,
方法一:逐个字符复制
#include fstream
std::ifstream input("in",ios::binary);
std::ofstream output("out",ios::binary);
char ch;
while (input.get(ch)) output ch;
注意:如果使用inputch读取字符,则必须先调用input.unsetf(ios::skipws)取消输入流默认的跳过空白符的输入格式,因为换行符是空白符的一种。
方法二:逐行复制
#include fstream
#include string
std::ifstream input("in",ios::binary);
std::ofstream output("out",ios::binary);
std::string line;
while (getline(input,line)) output line "\n";
注意:这里的代码有一个小小的缺陷,如果文件不是纯文本格式的文件,或者文本文件的最后没有换行符,那么会导致复制后的文件末尾添加了一个多余的换行符。
方法三:迭代器复制
#include fstream
#include iterator
#include algorithm
std::ifstream input("in",ios::binary);
std::ofstream output("out",ios::binary);
input.unsetf(ios::skipws);
copy(istream_iterator(input),istream_iterator(),ostream_iterator(output,""));
同样这里也有一个小技巧,输入流的格式默认为跳过空白字符,因此调用unsetf取消这个格式,才可保证正确的复制。
方法四:缓冲区复制
#include fstream
std::ifstream input("in",ios::binary);
std::ofstream output("out",ios::binary);
output input.rdbuf();
这里直接使用了输入流的缓冲区,因此没有引入额外的临时对象。
很显然,上述四种方法中,最后一种方法最简洁,由于直接操作输入流的缓冲区,从运行效率上来说,也比其他方法有着略微的优势(当然,由于操作系统可能提供了额外的基于设备的文件缓冲机制,也许你无法证实这一点)。因此,除非要对输入内容进行处理,直接复制文件推荐最后一种方法,既不容易出错,又能获得良好的性能。
以上是搜索的资料,希望对你有帮助
c++从txt文件读入数据到程序中出现问题,源代码如下:
(第二次补充)
我第二次审查的时候发现,你的读入还是有一定的问题的,我把它改成如此:
#include iostream
#include fstream
#include cstring //为了求字符串的长度
using namespace std;
int main()
{
ofstream output;
output.open("output.txt"); //新建一个output.txt
char a[50],b[50];
cout "请输入一串您想储存到计算机上的字符,并以“#”号键结束:"endl;
cin b; //直接读入B后再处理,以免逐个读入读乱掉
b[strlen(b)-1] = '\0'; //strlen是一个函数,包含在cstring里,意在求出b字符串的长度。
output b;
cout"您所输入的字符串:“"b"”已储存到计算机中。"endl;
output.close(); //在前面我写的程序中,我没有注意到这一点,要关闭文件。关闭文件就用fstream对象函数表示,close()
}
从output.txt读入就需要用到我们C++的一个类ifstream。它专门用来从文件当中读入数据的。其用法为:ifstream in ( "xxx.txt" ); 这里in是一个标识符,可以是任何合法的名称,xxx.txt是文件名称。这样,我们就可以用这个对象去完成你的任务了。第二次审查后,我的程序简略了很多。
ifstream in("output.txt");
in a;
for ( int i = 0; i strlen(a); i++ ) {
if ( a[i] = 'a' a[i] = 'z' ) cout static_castchar(a[i]-32); //如果是小写就转换。在你提出的另外一个问题中,有网友指出这样转换会令人迷惑。static_casttype(a)是将a转换为type类型,所以建议你用这个格式,就不容易乱了。
else cout a[i];
}
由于在for循环当中已经逐字从文件读入转换并输出,这里并不需要做任何事情。整个文件如下:
//change_from_file.cpp
#include iostream
#include fstream
#include cstring
using namespace std;
int main()
{
ofstream output;
output.open("output.txt");output.txt
char a[50],b[50];
cout "请输入一串您想储存到计算机上的字符,并以“#”号键结束:"endl;
cin b;
b[strlen(b)-1] = '\0';
output b;
cout"您所输入的字符串:“"b"”已储存到计算机中。"endl;
output.close();
ifstream in("g:/output.txt");
in a;
for ( int i = 0; i strlen(a); i++ ) {
if ( a[i] = 'a' a[i] = 'z' ) cout static_castchar(a[i]-32);
else cout a[i];
}
return 0;
}
另外,站长团上有产品团购,便宜有保证
c++ MFC ifstream 问题
ifstream是只能输入文件的, 而要拷贝肯定又要读又要写的, 所以肯定不能只用ifstream. 当然ifstream如果真地想让它写文件, 也可以, 但是任何一个合格的程序员是不会这样干的, 这样写出的程序可读性极低.
综上所述, 要想实现拷贝文件最少使用两个类, 而其中一个必然是ifstream, 另一个就是ofstream.
因为是C++实现, 所以要用面向对象的思想去写代码, 代码如下:
#include fstream
using namespace std;
class CFile //此类与MFC的CFile类重名, 若要应用于MFC, 需要修改类名.
{
ofstream fout;
ifstream fin;
CFile(char *in, char *out):fout(out), fin(in){} //构造函数
~CFile(){} //析构函数不需要执行任何操作
bool Copy()
{
if (!fout || !fin) //若打开文件失败
return false; //返回假
char data; //用来传输数据的变量
while(!fin.eof()) //判断源文件是否到文件尾, 若是退出循环
{
fin.read(data, 1); //读取数据
fout.write(data, 1); //保存数据
}
return true; //拷贝成功, 返回真
}
}; //不要掉了分号
ifstream源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于ifstream file、ifstream源代码的信息别忘了在本站进行查找喔。
版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;
相关推荐
- 05-01发布站源代码下载(下载源码的网站)[20240501更新]
- 05-01jeebbs源代码(jeesite代码生成)[20240501更新]
- 05-01企业导航条源代码(导航系统源码)[20240501更新]
- 05-01购物网站asp.net源代码(购物网站jsp完整源码)[20240501更新]
- 05-01wordpress运行代码(wordpress 源代码)[20240501更新]
- 05-01html5留言板源代码(java留言板代码)[20240501更新]
- 04-30火锅店网站源代码(火锅店网页)[20240430更新]
- 04-30关于数据录入查询系统asp源代码的信息[20240430更新]
- 04-30vb解密代码(vb源代码加密)[20240430更新]
- 04-30看matlab函数代码(怎么看matlab函数的源代码)[20240430更新]
取消回复欢迎 你 发表评论:
- 标签列表
- 最近发表
- 友情链接