2024年3月6日发(作者:)

C++文件输入输出流

文件操作在操作系统的底层中十分复杂。然而,C++已经为我们做了文件操作的绝大部分工作。程序员只要以流的概念来实施文件操作即可。

文件有两种,一种是文本文件,其内容总是与字符码表(如ASCII码)对应。另一种是二进制文件,可以将内容看成0/1串。

本次课我们只讨论C++中文本文件的读写。

一、 文件输入流类 ifstream

I ifstream是文件输入流类的类名。定义一个该类的对象,即可将该对象和一个文件建立联系。例如,

ifstream fin(“”)

表示定义一个文件输入流类的对象fin,并将该对象与当前目录下的文件建立联系,以后可以用该对象从中读取数据。

【例1 按行读取一个文本文件的所有内容,并输出到屏幕。】

#include

#include //使用ifstream类需要包含头文件 fstream

#include //使用string类需要包含头文件 string

using namespace std;

int main()

{

ifstream fin(""); //默认打开文本文件

string str;

while (getline(fin, str))

{

cout<

}

return 0;

}

getline(fin, str)表示从输入文件流中读入一行数据,放入string类型的变量str中。由于整行整行地读入,读入到str中时,文件中的每个换行符都被丢掉了。为了照文件原样输出,在cout流上输出str的同时,还要再补上一个回车符。

许多输入/输出语句都能返回操作状态(true或false),例如

if (cin>>a)

cout<

if (getline(in, str)

cout<

if (cin)

cin>>a; //若文件流状态正常,则输入

所以在循环读入数据时,常常将读入操作放在循环的条件判断上,这样既省事,又明白。

【例2 从文本文件中读取整数。假设有文本文件,其内容如下:

3

12

101

131

6

输出其中所有的偶数。】

#include

#include //使用ifstream类需要包含头文件 fstream

using namespace std;

int main()

{

ifstream in("");

int x;

while (in>>x)

{

if (x % 2 == 0)

cout<

}

return 0;

}

【例3 从文本文件中读取不同类型的数。假设有文本文件,其内容如下:

4

1 tom 20 170.5

2 jack 19 185.9

3 rose 21 165.3

4 white 19 170

该文件第一行的整数n表示接下来会有n行数据。统计其中所有同学的平均年龄和平均身高。】

#include

#include //使用ifstream类需要包含头文件 fstream

#include

using namespace std;

struct student

{

int num;

string name;

int age;

double height;

};

int main()

{

ifstream fin("");

student *stu;

int size;

double aveAge = 0, aveHeight = 0;

fin>>size;

stu = new student[size];

int i;

for (i = 0; i < size; i++)

{

fin>>stu[i].num>>stu[i].name>>stu[i].age>>stu[i].height;

aveAge += stu[i].age;

aveHeight += stu[i].height;

}

aveAge /= size;

aveHeight /= size;

cout<<"平均年龄:t"<

cout<<"平均身高:t"<

return 0;

}

二、 文件输出流类 ofstream

ofstream是文件输出流类的类名。定义一个该类的对象,即可将该对象和一个文件建立联系。例如,

ofstream fout(“”)

表示定义一个文件输出流类的对象fout,并将该对象与当前目录下的文件建立联系,以后可以用该对象向中输出数据。

文件输出流类对象的使用与cout的使用完全相同。

三、编程(来源:浙江工业大学/)

1、

ACM训练网站:A+B Problem

Time Limit:1000MS Memory Limit:1024K

Description:

Calculate a + b

Input:

The input will consist of a series of pairs of integers a and b,separated

by a space, one pair of integers per line, 0 0 means the end of the input,

and do not need to output.

Output:

For each pair of input integers a and b you should output the sum of a

and b in one line,and with one line of output for each line in input.

Sample Input:

1 5

0 0

Sample Output:

6

2、逆反的01串

Time Limit:1000MS Memory Limit:32768K

Description:

Fans是个ACM程序设计迷。有时侯,他表现出很强烈的逆反心理,你往东,他往西,你往南,他偏往北。这一次,不知道又是谁惹着他了,好端端的一个个01串,到了他的手里,都变成10串了。请你编个程序来模仿他的行为,将01串(长度≤200),全变成10串吧。

Sample Input:

0

1000000

Sample Output:

1

1111111

3、神奇的fans

Time Limit:1000MS Memory Limit:32768K

Description:

传说fans是一个数学天才。在他五岁那年,从一堆数字卡片中选出了4张卡片:5,7,6,8。这4个数字有什么神秘之处呢?如果把这4张卡片自左往右的排成:5,6,7,8。你就会发现:原来这4个数字构成了等差数列!当年fans选出了n组卡片,据说都能够构成等差数列。但是事实真的是这样吗?fans真的有这么神奇吗? n组数据就是fans选出的n组卡片,请你判断每一组卡片是否能构成等差数列,如果能够构成等差数列,输出“yes”,否则输出“no”。文件中第一个数为数据的组数n,表示后面有n行,每行中的第一个数为该组数据的元素个数m(1≤m≤100),其后是m个正整数(不会超出int的表示范围)。

Sample Input:

2

4 5 7 6 8

8 1 7 3 2 8 12 78 3

Sample Output:

yes

no

4、对称串

Time Limit:1000MS Memory Limit:32768K

Description:

有一些字串,有些对称,有些不对称,请将对称的串按长度排列。

Sample Input:

123321

123454321

123

321

sdfsdfd

121212

dd

Sample Output:

123321

dd

123454321