注册 登录
编程论坛 C++教室

读取文本条件,如何和文件关联起来

yi19860699 发布于 2010-05-20 14:33, 484 次点击
// sumafile.cpp -- functions with an array argument
#include <iostream>
#include <fstream>
#include <cstdlib>
const int SIZE = 60;
int main()
{
    using namespace std;
    char filename[SIZE];
    ifstream inFile;
    cout << "Enter name of data file: ";
    cin.getline(filename, SIZE);
    inFile.open(filename);
    if (!inFile.is_open())
    {
        cout << "Could not open the file: " << filename << endl;
        cout << "Program terminating.\n";
        exit(EXIT_FAILURE);
    }
    double value;
    double sum = 0.0;
    int cout = 0;
    inFile >> value;
    while (inFile.good())
    {
        ++count;
        sum += value;
        inFile >> value;
    }
    if (inFile.eof())
        cout << "End of file reached.\n" << endl;
    else if(inFile.fail())
        cout << "Input terminated by data mismatch.\n" << endl;
    else
        cout << "Input terminated for unknow reason.\n" << endl;
    if(count == 0)
        cout << "No data processed.\n" << endl;
    else
    {
        cout << "Items read: " << count << endl;
        cout << "Sum: " << sum << endl;
        cout << "Average: " << sum / count << endl;
    }
    inFile.close();
    return 0;
}
首先要创建一个包含数字的文本文件,要不然就不能运行该文件,如何将文本文件和程序关联起来??向大侠们求助
1 回复
#2
apull2010-05-20 14:40
文本文件和程序关联起来需要修改注册表,把注册表里.txt的运行程序改成你自己的就行。
不过一般杀毒软件都会认为这是恶意行为而阻止。
1