如何从c ++文件中读取特定数量的字符?

时间:2019-03-28 09:00:54

标签: c++

如果我们有一个存储日期的文件,然后是24个数字,则我们需要计算这些数字的平均值并将输出结果存储在另一个文件中。文件中的这些数字也是char形式,而不是int形式。我无法弄清楚如何读取每个数字并将其添加。 说该文件包含10条这样的行。 那么输出文件中必须包含10行日期,后跟平均值。

#include<fstream>
#include<iomanip>
#include<string>
using namespace std;

int main()
{
    fstream objtread,objtwrite;
    objtread.open("temperature.txt",ios::in);
    objtwrite.open("average.txt",ios::out);
    char temp[10];
    float temperature;


    while(objtread)
    {   float sum=0.0;

        objtwrite<<getline(temp,10);
        while()
        sum = sum + get(temperature);
        objtwrite<<setprecision(3)<<sum;

    }


}

2 个答案:

答案 0 :(得分:3)

您的代码无法编译,例如pip3 install -e .无效并且未定义 get()

  

计算这些数字的平均值,然后将输出结果放在另一个文件中

当前,您只计算总和,而错过了除法以获得平均值

  

输出文件必须包含10行日期,后跟平均值。

您错过了写日期


一种简单的方法是读取输入文件的每一行,然后使用 stringstream

提取日期和每个值之后的值。

您的代码中的提案:


    private List<cards> listItems;
    private Context context;


    public arrayAdapter(List<cards> listItems, Context context) {
        this.listItems = listItems;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        return new ViewHolder(v);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {
        final cards currentItem = listItems.get(i);

        viewHolder.name.setText(currentItem.getName());
        viewHolder.comment.setText(currentItem.getComment());


        Picasso.get().load(currentItem.getProfileImageUrl()).into(viewHolder.image);

        viewHolder.name.setText("this work"); // here it works

        viewHolder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               //Try this
                 currentItem.setName("New text");
                  notifyDataSetChanged();


               //viewHolder.name.setText("New text"); // this does not work
               // viewHolder.name.setVisibility(View.INVISIBLE); this does work
                Picasso.get().load(currentItem.getProfileImageUrl2()).into(viewHolder.image); // does not work
            }
        });

    }

    @Override
    public int getItemCount() {
        return listItems.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView name, comment, howMany;
        public ImageView image;


        /**
         * @param itemView
         */
        ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            image = (ImageView) itemView.findViewById(R.id.image);
            comment = (TextView) itemView.findViewById(R.id.commentText);
        }
    }


}

编译和执行:

while()

答案 1 :(得分:0)

一种稍有不同的方法是将每个数字存储在std::vector中,然后使用std::accumulate除以该向量vector::size()的大小来得出平均值。

int main()
{
    std::ifstream obj_read("in.txt");

    if( !obj_read.is_open() )
        return EXIT_FAILURE;

    std::string line;
    while (std::getline(obj_read, line))
    {
        std::cout<< line << " = ";

        line.erase(line.begin(),line.begin()+line.find(" ")+1);

        std::stringstream s(line);
        std::vector<double> numbers;
        double v=0.0;

        while (s >> v)
            numbers.push_back(v);

        auto average = std::accumulate(std::begin(numbers),std::end(numbers),0.0) / numbers.size();
        std::cout<< average <<std::endl;
    }

    obj_read.close();
    return EXIT_SUCCESS;
}

结果是:

2019/02/28 20.44 20.25 20.14 20.02 19.87 19.66 19.41 19.13 19.51 20.44 21.82 22.92 23.49 23.60 22.72 22.54 22.28 21.62 20.34 19.05 20.39 19.72 19.11 19.12 = 20.7329
2019/03/01 19.12 19.12 19.12 19.00 19.04 19.14 19.55 19.77 20.06 20.16 20.77 21.03 21.09 21.53 21.44 21.15 20.79 20.26 19.76 19.43 20.55 20.39 20.26 20.21 = 20.1142
2019/03/02 20.13 19.91 19.63 19.68 19.81 20.07 20.41 20.16 20.99 21.91 22.47 23.36 23.26 23.27 23.20 23.20 22.80 21.95 21.34 21.18 21.07 21.16 21.15 21.14 = 21.3854
2019/03/03 21.07 20.97 20.92 20.95 20.97 20.98 20.98 21.15 21.45 22.00 22.37 23.28 23.31 23.05 23.18 23.06 22.94 22.66 21.64 20.67 21.00 20.47 20.07 19.75 = 21.6204
2019/03/04 19.41 19.02 18.76 19.16 18.99 18.90 18.80 18.86 19.50 20.57 20.87 21.00 21.94 22.81 23.11 23.10 22.01 22.32 21.84 21.63 21.52 21.57 21.60 21.61 = 20.7875
2019/03/05 21.57 21.49 21.44 21.37 21.34 21.44 21.53 21.85 22.54 23.08 23.49 23.54 23.67 24.30 24.39 25.75 24.11 23.82 23.27 22.83 21.78 21.79 21.79 21.76 = 22.6642
2019/03/06 21.72 21.72 21.75 21.93 22.09 22.11 21.68 21.09 21.62 21.81 22.35 22.25 22.72 23.34 23.21 23.49 23.19 22.73 22.43 22.01 18.50 17.11 16.95 16.70 = 21.4375
2019/03/07 16.62 16.48 16.44 16.43 16.53 16.87 17.23 17.71 18.15 18.47 19.02 19.39 19.62 19.86 19.96 19.66 19.25 18.82 18.36 17.97 16.84 17.00 17.17 17.22 = 17.9613
2019/03/08 17.17 16.78 16.29 15.73 15.38 15.38 15.53 15.78 16.15 16.66 17.29 17.95 18.38 18.63 18.83 18.96 19.05 19.08 19.10 19.14 18.36 18.63 18.89 19.14 = 17.595
2019/03/09 19.34 19.56 19.76 19.91 20.04 20.19 20.30 20.42 20.69 21.06 21.72 22.17 22.61 23.37 23.66 23.44 23.31 23.07 22.76 22.41 16.59 16.72 16.81 16.87 = 20.6992
2019/03/10 16.86 16.88 16.85 16.70 16.58 16.45 16.31 16.10 16.20 16.44 16.75 17.07 17.21 17.19 17.27 17.05 16.84 16.75 16.69 16.59 16.53 16.43 16.14 15.98 = 16.6608
2019/03/11 16.07 16.17 16.31 16.42 16.46 16.48 16.47 16.54 17.30 18.37 19.64 20.53 21.11 21.42 21.50 21.35 20.68 20.37 19.87 19.50 17.63 17.39 17.25 17.79 = 18.4425
2019/03/12 18.37 18.61 18.39 17.70 17.40 17.99 17.14 17.20 19.31 20.27 20.94 21.80 22.30 22.54 22.58 22.45 22.15 21.42 20.37 19.32 19.61 19.29 19.01 18.79 = 19.7896
2019/03/13 18.56 18.36 18.23 18.14 18.11 18.04 17.89 18.32 20.07 22.39 23.13 23.71 23.98 23.96 23.89 23.67 23.38 22.94 22.08 21.66 21.53 21.46 21.37 21.24 = 21.0879