如何获得C++;读取文件 我是C++初学者。这是我正在完成的任务,因此我必须使用函数problem1(),因为我需要创建另一个函数来查找max和min。以下文本文件是为我提供的任务

如何获得C++;读取文件 我是C++初学者。这是我正在完成的任务,因此我必须使用函数problem1(),因为我需要创建另一个函数来查找max和min。以下文本文件是为我提供的任务,c++,C++,我很难让程序读取包含一组数字的文本文件,例如: [253676114] [527160277] [364193169] [246651378] [536479695] [56954883] 我的代码是: using namespace std; void problem1(string); int main() { string s= "data2.txt"; problem1(s); return 0; } void problem1(

我很难让程序读取包含一组数字的文本文件,例如:

[253676114]

[527160277]

[364193169]

[246651378]

[536479695]

[56954883]

我的代码是:

using namespace std;

void problem1(string);

int main()
{
    string s= "data2.txt";
    problem1(s);
    return 0;
}
    
void problem1(string s)
    {
        double num, sum = 0, avg;
        int count=0;
        ifstream file;
        file.open(s);
        if (file.is_open())
        {
            while(file>>num)
            {
                sum= sum + num;
                count++;
                
            }
    avg= sum/count;
        
        cout <<"Average is " << avg << endl;
        file.close();
        
    }

    }
使用名称空间std;
无效问题1(字符串);
int main()
{
字符串s=“data2.txt”;
问题1(s);
返回0;
}
无效问题1(字符串s)
{
双数值,和=0,平均值;
整数计数=0;
ifstream文件;
文件。打开;
if(file.is_open())
{
while(文件>>num)
{
sum=sum+num;
计数++;
}
平均值=总和/计数;

cout如果您查看输入文件的一行,那么您将看到以下内容

  • 开口方括号
  • 第一个数字
  • 逗号
  • 第二个数字
  • 逗号
  • 第三个数字
  • 结束括号
如果要读取文件中的行和所有行,则必须读取所有内容。对于不感兴趣的字符,可以使用
char temp
临时变量并将这些字符读入其中,如下所示:

fileStream >> temp >> first >> temp >> second >> temp >> third >> temp;
当然,您也可以使用更多“说话”和单独的变量,如:

fileStream >> openingBracket >> first >> comma1 >> second >> comma2 >> third >> closingBracket;
然后,如果变量包含预期值,还可以在
if
语句中进行验证:

if (openingBracket == '[' and comma1 == ',' and comma2 ==',' and closingBracket==']') ....
但现在就忘了这一点

因此,现在我们知道如何读取一行。如果我们想读取所有行,我们将上述语句放入
while
中。然后将运行该语句,直到它到达文件结尾或发生其他错误

在while循环体中,您可以进行计算。在while循环体之后,您可以将得到的平均值作为双倍值输出

请注意:

  • std::ifstream的构造函数将自动为您打开文件。析构函数将自动关闭该文件
  • 流的bool运算符被覆盖。如果状态为ok,或者如果设置了一个状态位,则返回。因此
    if(fileStream)
    足以进行检查
  • 提取器操作符
    >
    返回对流的引用。因此,如果您编写
    fileStream>>openingBracket
    ,则结果将是“fileStream”,这将用于下一次提取操作
    fileStream>>第一次
    ,依此类推。最后,您将得到类似于
    的内容(fileStream)
    。此操作将一直运行,直到设置了“eof”或其他故障位为止(请参阅上面的布尔运算符)
您的整个程序可以如下所示:

#include <iostream>
#include <fstream>
#include <string>

void problem1(std::string fileName) {

    // Open the file
    std::ifstream fileStream{ fileName };

    // Check, if everything is ok with the stream
    if (fileStream) {
        // These are dummy variables
        char openingBracket{}, comma1{}, comma2{}, closingBracket{};
        // The values in one line
        int first{}, second{}, third{};

        // For summing up and claculating the average
        int sum{}, counter{};

        // Read data, as long as there are any
        while (fileStream >> openingBracket >> first >> comma1 >> second >> comma2 >> third >> closingBracket) {

            // Do the necessary calculations
            sum += first + second + third;
            counter += 3;
        }
        // Output avaerage value as double
        std::cout << "Average is " << static_cast<double>(sum) / static_cast<double>(counter) << '\n';
    }
}

int main() {
    std::string s = "data2.txt";
    problem1(s);
    return 0;
}
#包括
#包括
#包括
无效问题1(标准::字符串文件名){
//打开文件
std::ifstream fileStream{fileName};
//检查流是否一切正常
如果(文件流){
//这些是虚拟变量
字符openingBracket{},comma1{},comma2{},closingBracket{};
//一行中的值
int第一{},第二{},第三{};
//用于总结和计算平均值
整数和{},计数器{};
//读取数据,只要有
而(文件流>>打开支架>>第一个>>通信A1>>第二个>>通信A2>>第三个>>关闭支架){
//做必要的计算
总和+=第一+第二+第三;
计数器+=3;
}
//将平均值输出为双精度
std::cout>第一个>>通信A1>>第二个>>通信A2>>第三个>>闭合括号){
//做必要的计算
总和+=第一+第二+第三;
计数器+=3;
如果(第一个>最大值)最大值=第一个;
如果(秒>最大值)最大值=秒;
如果(第三个>最大值)最大值=第三个;
如果(第一次<最小值)最小值=第一次;
如果(秒<分)分=秒;
如果(第三次<最小值)最小值=第三次;
}
//输出平均值为双倍

std::cout如果您查看输入文件的一行,那么您将看到以下内容

  • 开口方括号
  • 第一个数字
  • 逗号
  • 第二个数字
  • 逗号
  • 第三个数字
  • 结束括号
如果要读取文件中的行和所有行,则必须读取所有内容。对于不感兴趣的字符,可以使用
char temp
临时变量并将这些字符读入其中,如下所示:

fileStream >> temp >> first >> temp >> second >> temp >> third >> temp;
当然,您也可以使用更多“说话”和单独的变量,如:

fileStream >> openingBracket >> first >> comma1 >> second >> comma2 >> third >> closingBracket;
然后,如果变量包含预期值,还可以在
if
语句中进行验证:

if (openingBracket == '[' and comma1 == ',' and comma2 ==',' and closingBracket==']') ....
但现在就忘了这一点

因此,现在我们知道如何读取一行。如果我们想读取所有行,我们将上述语句放入
while
中。然后将运行该语句,直到它到达文件结尾或发生其他错误

在while循环体中,您可以进行计算。在while循环体之后,您可以将得到的平均值作为双倍值输出

请注意:

  • std::ifstream的构造函数将自动为您打开文件。析构函数将自动关闭该文件
  • 流的bool运算符被覆盖。如果状态为ok,或者如果设置了一个状态位,则返回。因此
    if(fileStream)
    足以进行检查
  • 提取器操作符
    >
    返回对流的引用。因此,如果您编写
    fileStream>>openingBracket
    ,则结果将为“fileStream”,这将用于下一次提取