2016-04-25 7 views
-7

Die Datei ist eine Textdatei mit dem Namen TotalMonthlyRainfall2014.txt und es enthält die folgenden in einer einzigen Zeile:Wie lese ich eine Zeile mit 12 Zahlen aus einer Datei und speichere sie in einem Array?

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

ich die Zahlen aus der Datei lesen möchten, und speichern sie in einem einzigen Array namens monthRain. So wird monthRain [0] 0,33, monthRain [1] wird 0,41 und so weiter. Diese

ist das, was ich bisher:

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

ich die Frage erraten ist, wie man richtig die Zahlen zu speichern, in Array monthRain.

+0

Und welche Probleme haben Sie speziell mit diesem Code? –

+0

http://stackoverflow.com/questions/14516915/read-numeric-data-from-a-text-file-in-c- Duplikatsfrage – bodangly

+1

Mögliches Duplikat von [Wie man aus Zahlen von Datei in Array liest] (http: //stackoverflow.com/questions/36850600/how-to-read-from-numbers-from-file-into-array) – LogicStuff

Antwort

0

Dies sollte Ihr Problem beheben. Es analysiert alle Zeilen und platziert jede Zeile in einem String-Stream. Es analysiert dann den String-Stream und streamt die Elemente in Doubles. Es sollte für jede Kombination von Zeilen und Spaltenelementen funktionieren.

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

Sind Sie sicher, dass dies die richtige Frage für Ihre Antwort ist? –

+0

Ja, ich weiß, dass es allgemeiner ist, aber ich denke, wenn man sich die Mühe macht, dies zu tun, könnten sie genauso gut Code haben, den sie wiederverwenden können. Habe gerade festgestellt, dass ich die Konvertierung nicht in das Format, das das OP will, bereitgestellt habe: Das c-Array kann immer aus dem Vektor abgerufen werden: double * monthRain = & myData.at (0) [0] – nikaza

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(); 
} 

Fortschritt! Nun, wenn in 0 ist: cout < < setprecision (2) < < festen < < showpoint < < monthRain [0] < < '\ n'; es gibt 0.33 aus, was richtig ist, aber es gibt es 12 Mal aus.

+0

Sie müssen 'monthRain [i]' anstelle von 'monthRain [0]' verwenden. –

+0

Denken Sie eine Weile an 'monthRain [0]'. – user4581301