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.
Und welche Probleme haben Sie speziell mit diesem Code? –
http://stackoverflow.com/questions/14516915/read-numeric-data-from-a-text-file-in-c- Duplikatsfrage – bodangly
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