C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  117 Posts :: 2 Stories :: 61 Comments :: 0 Trackbacks

常用链接

留言簿(8)

搜索

  •  

最新评论

阅读排行榜

评论排行榜


第一个程序:将内容写入txt文本

#include "stdafx.h"
#include 
<iostream>
#include 
<fstream>  //for file I/O
using namespace std;
int main(int argc, char* argv[])
{
    
char automobile[50];
    
int year;
    
double a_price;
    
double d_price;

    ofstream outFile; 
//creat object for output

    
/* associate with a file在这里,程序运行之前,文件carinfo.txt并不存在。
     * 在这种情况下,方法open()将新建一个名为carinfo.txt的文件。运行该程序
     * 之后,文件carinfo.txt将存在。默认情况下,open()将首先截短该文件,即
     * 将其长度截短到零——丢弃原有内容,然后将新的输出加入到该文件中。
     *
     * 打开已有的文件,以接受输出时,默认将它其长度截短为零,因此原来的内容将丢失。
     
*/

    outFile.open(
"carinfo.txt"); 
    

    
    cout
<<"Enter the make and model of auto mobile: ";
    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 cout

    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;
    cout.precision(
2);
    cout.setf(ios_base::showpoint);
    outFile
<<"Make and mode: "<<automobile<<endl;
    outFile
<<"Year: "<<year<<endl;
    outFile
<<"Was asking $"<<a_price<<endl;
    outFile
<<"Now asing $"<<d_price<<endl;

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

第二个程序:读取txt文本的内容 
      下面程序打开用户指定的文件,读取其中的数字,然后指出文件中包含多少个值以及它们的和与平均值。正确地设计输入循环至关重要。
       这个程序我运行的时候出了一个问题,就是未能找到最后结果计算,如下图所示,
      正确的运行结果应该是:
       很有意思,可以研究哈,我在csdn发帖询问了,可参考其帖子:txt文件读取问题
       程序代码如下:
#include "stdafx.h"
#include 
<iostream>
#include 
<fstream>    //file I/O support
#include <cstdlib>  //support for exit()
using namespace std;
const int SIZE =60;
int main(int argc, char* argv[])
{
    
char filename[SIZE];
    ifstream inFile;  
//object for handling file input

    cout
<<"Enter name of data file: ";
    cin.getline(filename, SIZE);
    inFile.open(filename); 
//associate inFile with a file
    if(!inFile.is_open())  //failed to open file
    {
        cout
<<"Could not open the file "<<filename<<endl;
        cout
<<"Program terminating.\n";
        exit(EXIT_FAILURE);
    }

    
double value;
    
double sum=0.0;
    
int count=0;  //number of items read

    inFile
>>value;  //get first value
    while(inFile.good()) //while input good and not at EOF
    {
        
++count;            //one more item read
        sum+=value;            //calculate runing total
        
//cout<<"第"<<count<<"个数="<<value<<endl;
        inFile>>value;        //get next value
    }

    
//cout<<value<<endl;
    if(inFile.eof())
        cout
<<"End of file reached.\n";
    
else if(inFile.fail())
        cout
<<"Input terminated by data mismatch.\n";
    
else
        cout
<<"Input terminated for unknown reason.\n";
    
if(count==0)
        cout
<<"No data processed.\n";
    
else
    
{
        cout
<<"Items read: "<<count<<endl;
        cout
<<"Sum: "<<sum<<endl;
        cout
<<"Average: "<<sum/count<<endl;
    }

    inFile.close();        
//finished with the file
    return 0;
}
posted on 2010-02-11 17:10 烟皑 阅读(391) 评论(0)  编辑 收藏 引用 所属分类: C++ primer plus学习笔记

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