【问题标题】:How do I read a line of 12 numbers from a file and store it into an array?如何从文件中读取一行 12 个数字并将其存储到数组中?
【发布时间】:2016-08-19 10:55:51
【问题描述】:

该文件是一个名为 TotalMonthlyRainfall2014.txt 的文本文件,它在一行中包含以下内容:

0.33 0.41 1.45 1.74 3.40 3.26 0.98 4.34 0.06 2.09 2.13 1.13

我想从文件中读取数字并将它们存储到一个名为monthRain 的数组中。所以 monthRain[0] 将为 0.33,monthRain[1] 将为 0.41,依此类推。

这是我目前所拥有的:

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
//global variable
const int months = 12;
const string FILENAME = "TotalMonthlyRainfall2014.txt";

int main()
{
    ifstream inFile;        //input file stream
    float monthRain[months];

                    //open the file
    inFile.open(fileName.c_str());
    //loop through and get data from file
    for (int i = 0; i < months && (inFile >> monthRain); i++)
    {
       cout << setprecision(2) << fixed << showpoint << monthRain[i];
    }

    inFile.close();
}

我想问题是如何将数字正确存储到数组monthRain中。

【问题讨论】:

标签: c++ arrays file int


【解决方案1】:

这应该可以解决您的问题。它将解析所有行并将每一行放在一个字符串流中。然后它解析字符串流并将元素流式传输为双精度。它应该适用于行和列元素的任意组合。

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;
int main(){

// Open file
ifstream fileStream;
fileStream.open("myIniFile.txt");

// Initialize variables
string lineString;
int counter=0;
vector<vector<double> > myData; // Matrix which holds all data
vector<double> myLine; // Temporary vector to hold each row
double currentNumber;

// Read file
while(!fileStream.eof()){
    ++counter;        
    getline(fileStream,lineString); // Stream this line in a string
    if (lineString!=""){ // If empty, exit            
        cout<<"Line #"<<counter<<" : "; // Print
        cout<<lineString<<endl;         // output            
        stringstream myS_stream(lineString);
        while(myS_stream>>currentNumber){ // Important as a simple break condition will read the last element twice
            cout<<"\tFound double number in string stream : "<< currentNumber<<endl;
            myLine.push_back(currentNumber);
        }
        myData.push_back(myLine);
        myLine.clear();
    }
}
fileStream.close(); // Close file

// Print your data
cout<<"\nMy data is : "<<endl;
for (auto row : myData){
    cout<<"\t";
    for (auto element : row){
        cout<<" "<<element;
    }
    cout<<endl;
}

// Convert to the format you want, but I suggest using std::vector
// if you can help it
double *monthRain = &myData.at(0)[0];

return 0;
}

【讨论】:

  • 您确定这是您回答的正确问题吗?
  • 是的,我知道它更通用,但我认为如果要费心去做这件事,他们还不如拥有可以重用的代码。刚刚意识到我没有提供转换为 OP 想要的格式:总是可以从向量中检索 c 数组:double *monthRain = &myData.at(0)[0]
【解决方案2】:
// Headers
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

// Global variables
const int months = 12;
const string FILENAME = "TotalMonthlyRainfall2014.txt";

int main()
{
ifstream inFile;        //input file stream
float monthRain[months];

                   //open the file
inFile.open(FILENAME.c_str());
//loop through and get data from file
for (int i = 0; i < months && (inFile >> monthRain[i]); i++)
{
    cout << setprecision(2) << fixed << showpoint << monthRain[0] << '\n';
}

inFile.close();
}

进步!现在只要 0 在: cout

【讨论】:

  • 你必须使用monthRain[i] 而不是monthRain[0]。
  • 想一想monthRain[0]。
猜你喜欢
  • 1970-01-01
  • 2023-01-19
  • 2020-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-10
  • 2020-12-26
相关资源
最近更新 更多