posts - 15,  comments - 0,  trackbacks - 0
对于文件输入,C++使用类似于cout的东西。下面来复习一些有关将cout用于控制台输出的基本事实,为文件输入做准备:
    ● 必须包含头文件iostream。
    ● 头文件iostream定义了一个用处理输出的ostream类。
    ● 头文件iostream声明了一个名为cout的ostream变量(对象)。
    ● 必须指明名称空间std:例如,为引用元素cout和endl,必须使用编译指令using或前缀std::。
    ● 可以结合使用cout和操作符“>>”来显示各种类型的数据。

    文件输出与此极其相似:
    ● 必须包含头文件fstream。
    ● 需要声明一个或多个ofstream变量(对象),并以自己喜欢的方式对其进行命名,条件是遵守常用的命名规则。
    ● 必须指明名称空间std:例如,为引用元素ofstream,必须使用编译指令using或前缀std::。
    ● 需要将ofstream对象与文件关联起来。为此,方法之一是使用open()方法。
    ● 使用完文件后,应使用close()方法将其关闭。
    ● 可结合使用ofstream对象和操作符>>来输出各种类型的数据。
   
    注意,虽然头文件iostream提供了一个预先定义好的名为cout的ostream对象,但您必须声明自己的ofstream对象,为其命名,并将其同文件关联起来。下面演示了如何声明这种对象。
   
        ofstream outFile;   //outFile an ofstream object
        ofstream fount;    //fout an ofstream object

    下面演示了如何将这种对象与特定的文件关联起来:

        outFile.open("fish.txt"); //outFile used to write to the fish.txt file
        char filename[50];
        cin >> filename;   //user specifies a name
        fout.open(filename);   //fout used to read specified file

    注意,方法open()接受一个C-风格字符串作为参数,这可以是一个字面字符串,也可以是存储在数组中的字符串。
    下面演示了如何使用这种对象:

        double wt = 125.8;
        outFile << wt;    //write a number to fish.txt
        char line[81] = "Objects are closer than they appear.";
        fin << line << endl;   //write a line of text

    重要的是,声明一个ofstream对象并将其同文件关联起来后,便可以像使用cout那样使用它。所有可用于cout的操作和方法(如<<、enld和setf())都可用于ofstream对象(如前述范例中的outFile和fout)。

    总之,使用文件输出的主要步骤如下:
    1. 包含头文件fstream。
    2. 创建一个ofstream对象。
    3. 将该ofstream对象同一个文件关联起来。
    4. 就像使用count那样使用该ofstream对象。
    程序清单6.15中的程序演示了这种方法。它要求用户输入信息,然后将信息显示到屏幕上,再将这些信息写入到文件中。读者可以使用文本编辑器来查看该输出文件的内容。

    程序清单6.15 outfile.cpp
    //outfile.cpp -- writing to a file
    #include <iostream>
    #include <fstream>

    int main() {
        using namespace std;
        char automobile[50];
        int year;
        double a_price;
        double d_price;
        ofstream outFile;   //create object for output
        outFile.open("carinfo.txt"); //associate with a file

        cout << "Enter the make and model of automobile:";
        cin.getline(automobile, 50);
        cout << "Enter the model year:";
        cin >> year;
        cout << "Enter the original asking price:";
        cin >> a_price;
        d_price = 0.913 * a_price;

        // display information on screen with count

        cout << fixed;
        cout.precision(2);
        cout.setf(ios_base::showpoint);
        cout << "Make and model:" << automobile << endl;
        cout << "Year:" << year << endl;
        cout << "Was asking $" << a_price << endl;
        cout << "Now asking $" << d_price << endl;

        // now do exact same things using outFile instead of cout

        outFile << fixed;//如果输出的数据是浮点数.则按定点表示法输出
        outFile.precision(2);//按照指定的精度控制浮点数输出
        outFile.setf(ios_base::showpoint);//为输出的数据强制加小数点以及小数点后
        outFile << "Make and model:" << automobile << endl;
        outFile << "Year:" << year << endl;
        outFile << "Was asking $" << a_price << endl;
        outFile << "Now asking $" << d_price << endl;

        outFile.close(); //done with file
        return 0;
    }  

    该程序的最后一部分与cout部分相同,只是将cout替换为outFile而已。下面是该程序的运行情况:
    Enter the make and model of automobile: Flitz Pinata
    Enter the model year: 2001
    Enter the original asking price :28576
    Make and model: Flitz Pinata
    Year: 2001
    Was asking $28576.00
    Now asking $26089.89
   
    屏幕输出的是使用cout的结果。如果您查看该程序的可执行文件所在的目录,将看到一个名为carinfo.txt的新文件,其中包含使用outFile生成的输出。
   
    程序说明
    在程序清单6.15的程序中,声明一个ofstream对象后,便可以使用方法open()来将该对象同一个特定的文件关联起来:

        ofstream outFile;   //create object for output
        outFile.open("carinfo.txt"); //associate with a file

    程序使用完该文件后,应该将其关闭:

        outFile.close();

    注意,方法close()不需要使用文件名作为参数,这是因为outFile已经同特定的文件关联起来了。如果您忘记关闭文件,程序正常终止时将自动关闭它。
    outFile可使用cout可使用的任何方法。它不但能够使用操作符<<,还可以使用各种格式化方法,如setf()和precision()。这些方法只影响调用它们的对象。例如,对于不同的对象,可以提供不同的值:

        cout.precision(2); //use a precision of 2 for the display
        outFile.porecision(4); //use a precision of 4 for file output

    读者需要记住的重点是,创建好ofstream对象(如outFile)后,便可以像使用cout那样使用它。
    回到open()方法:

        outFile.open("carinfo.txt");

    在这里,该程序运行之前,文件carinfo.txt并不存在。在这种情况下,方法open()将新建一个名为carinfo.txt的文件。如果在此运行该程序,文件carinfo.txt将存在,此时情况将如何呢?默认情况下,open()将首先截断该文件,即将其长度截短到零——丢其原有的内容,然后将新的输出加入到该文件中。第17章将介绍如何修改这种默认行为。

        警告:打开已有的文件,以接受输出时,默认将它其长度截短为零,因此原来的内容将丢失。

    打开文件用于接受输入时可能失败。例如,指定的文件可能已存在,但禁止对其进行访问。因此细心的程序员将检查打开文件的操作是否成功,这将在下一个例子中介绍。

posted on 2010-10-07 19:10 王秋林 阅读(1375) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(1)

随笔档案(15)

搜索

  •  

最新评论

阅读排行榜

评论排行榜