如何在空间分隔的信息由在C文件中读取++ [英] How to read in space-delimited information from a file in c++

查看:108
本文介绍了如何在空间分隔的信息由在C文件中读取++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个文本文件,我将有一个包含一系列数字的线,用空格分隔每个数字。我将如何读这些数字和他们都存储在一个阵列?


解决方案

 的std :: ifstream的文件(文件名);
的std ::矢量<&INT GT;阵列;
INT编号;
而(文件>>数){
    array.push_back(数);
}

In a text file I will have a line containing a series of numbers, with each number separated by a space. How would I read each of these numbers and store all of them in an array?

解决方案

std::ifstream file("filename");
std::vector<int> array;
int number;
while(file >> number) {
    array.push_back(number);
}

这篇关于如何在空间分隔的信息由在C文件中读取++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆