ifstream和ofstream的用法

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 = {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 《《 “\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++;
        if (c 》= ’A’ && c 《= ’Z’)
            counts++;
    }
}

源程序在 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++ builder 文件读写 在C++ builder里用ifstream和ofstream

新版的c++builder默认使用unicode编码。
可以使用AnsiString类,如
AnsiString ain = “datain.txt“;
ifstream in(ain);
或者使用wifstream类。

ofstream ifstream 使用

你先看一下你有没有包含头文件fstream
然后你看一下你有没有对ifstream和ofstream的对象进行open()和close()操作,其他就没什么了,都和cout和cin的操作一样

C++中, 有个时候关键词定义有fstream 有个时候是 ifstream ofstream 这几个有什么区别

ifstream 输入文件流 input file stream 用于从文件读数据(从文件读入)
ofstream 输出文件流 output file stream 用于向文件写数据(输出到文件)
fstream 文件流,可以输入也可以输出,是上面两个的合体

C++中用ifstream与ofstream,读取的文件的格式应该怎么写啊

在fstream类中,成员函数open()实现打开文件的操作,从而将数据流和文件进行关联,通过ofstream,ifstream,fstream对象进行对文件的读写操作

函数:open()

《span style=“font-family:Times New Roman;font-size:16px;“》
public member function
void open ( const char * filename,
            ios_base::openmode mode = ios_base::in | ios_base::out );
void open(const wchar_t *_Filename,
    ios_base::openmode mode= ios_base::in | ios_base::out,
    int prot = ios_base::_Openprot);
《/span》

参数: filename   操作文件名

           mode        打开文件的方式

           prot         打开文件的属性    

很多程序中,可能会碰到ofstream out(“Hello.txt“), ifstream in(“…“),fstream foi(“…“)这样的的使用,并没有显式的去调用open()函数就进行文件的操作,直接调用了其默认的打开方式,因为在stream类的构造函数中调用了open()函数,并拥有同样的构造函数,所以在这里可以直接使用流对象进行文件的操作,默认方式如下:

《span style=“font-family:Times New Roman;font-size:16px;“》
ofstream out(“…“, ios::out);
ifstream in(“…“, ios::in);
fstream foi(“…“, ios::in|ios::out);
《/span》

当使用默认方式进行对文件的操作时,可以使用成员函数is_open()对文件是否打开进行验证

C++文件流问题 ifstream ofstream fstream有什么区别0.0

ifstream是文件输入流,用于文件读打开;
ofstream是文件输出流,用于文件写打开;
fstream是文件输入输出流,用于文件读写打开。

谁能举几个简单的例子说说c++文件流ifstream和ofstream怎么用

去C++的网站,基本各个库函数的例子都有,不过需要你会英语。请看参考资料链接。
// opening and closing a file
#include 《iostream》
#include 《fstream》
using namespace std;
int main () {
ofstream outfile;
outfile.open (“test.txt“);
// 》》 i/o operations here 《《
outfile.close();
return 0;
}
// print the content of a text file.
#include 《iostream》
#include 《fstream》
using namespace std;
int main () {
ifstream infile;
infile.open (“test.txt“, ifstream::in);
int ch = infile.get();
while (infile.good()) {
cout 《《 (char) ch;
ch = infile.get();
}
infile.close();
return 0;
}

关于ofstream和ifstream

简单的来说,
你用open函数来说应该只有两个参数;
.open(“文件名“,“文件模式“);
如果是ofstream的话。模式应该是ios::out,表示写入文件,但ofstream一般都是默认的ios::out,所以你写ff.open(“f:\\1.txt“,ios::out);和ff.open(“f:\\1.txt“);是一样的,除非你要追加的形式就一定要写上去,也就是ff.open(“f:\\1.txt“,ios_base::app);
如果是ifstream的话。模式应该是ios::in,表示读取文件,但ifstream一般都是默认的ios::in,所以你写ff.open(“f:\\1.txt“,ios::in);和ff.open(“f:\\1.txt“);是一样的,除非你要追加的形式就一定要写上去,也就是ff.open(“f:\\1.txt“,ios_base::app);
还有不懂的继续交流~~~

请讲解一下cin,cout各个函数的作用,还有类ifstream,ofstream里面的函数作用

cin,cout常用的函数有:
cin 》》 //可连续使用;以空白(包括空格、回车、TAB)为分隔符
cin.get(char c) //可连续使用;获取任意单个字符,包括空白
cin.getline(char* buf, sizeof(buf), ’\n’) //可连续使用;获取一行,到最后指定的字符结束,可包括空白,默认回车
cin.gcount() //计数
cin.read(char* buf, sizeof(buf)) //可连续使用;读取指定大小的内容,包括空白;第一个参数必须为char*,可用强制转换
cin.ignore(1000, ’\n’) //忽略指定大小的内容,到制定字符结束忽略;常用来清空缓冲区
cin.clear() //清楚错误状态;常后跟ignore
if(!cin) //判断是否出错;cin为false则出错
cin.peek() //查看缓冲区下一个字符,但是不读取,即用get时候还可以读到
cin.putback() //将上一个读回的字符返回缓冲区,不常用
cout 《《
控制符:endl, flush ….
cout.put(char)
cout.width(int) //一次性命令
cout.fill(char)
cout.precision(int)
cout.setf(ios::…)
cout.unsetf(ios::…)
ifstream,ofstream是两个类,分别是输入文件流,输出文件流。负责从文件中读取内容,或者向文件写入内容。
给你俩网址看看吧:
http://blog.csdn.net/lishengwei/archive/2008/05/22/2470706.aspx
http://blog.csdn.net/zhourongbiao/archive/2006/08/29/1137384.aspx

c++ ifstream 和 ofstream 名子含义的理解

你理解错了,
输入文件流是从文件里面把数据读取到流中.
输出就是把流里的东西输出到文件里.
cin/cout也是一样的
cin把I/O的输入读取到流中….
cout把流中的东西输出到I/O上.
文件!=文件流,明白不?