如何从文件中读取12行数并将其存储到数组中?

时间:2016-04-25 20:57:21

标签: c++ arrays file int

该文件是名为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中。

2 个答案:

答案 0 :(得分:0)

// 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&lt;&lt; setprecision(2)&lt;&lt;固定&lt;&lt; showpoint&lt;&lt; monthRain [0]&lt;&lt; '\ n'; 它输出0.33这是正确的,但它输出12次。

答案 1 :(得分:0)

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

#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;
}