468,513 Members | 1,866 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

C++ reading data from file into array

Hi, I'm a newbie at C++. I am just trying to read a file with three columns of data into arrays and write it back out, to see how arrays work.

This runs with no errors, but the output doesn't look like the input, so I think I just have something specified incorrectly, but not sure what.

My input:
1999 1 3
1999 2 6
1999 4 5

My program:

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

void OpenFile(ifstream& fin);

void OpenFile(ofstream& fout);

void getInput(ifstream& fin,
int year[],
int month[],
int rain_in_month[]);

int main()
{
using namespace std;

//Variable declarations
const int MONTHS_IN_FILE = 36; //three years of data in file
int year[MONTHS_IN_FILE];
int month[MONTHS_IN_FILE];
int rain[MONTHS_IN_FILE];
ifstream fin; //naming the input file for the program
ofstream fout; //naming the output file for the program


fout.setf(ios::fixed);
fout.setf(ios::showpoint);
fout.precision(1);

//Open the input file
fin.open("rainInput2.txt");
OpenFile(fin);

//Open the output file
fout.open("rainOutput2.txt");
OpenFile(fout);

//Process the records from the input file until the end of file is reached
while (!fin.eof())
{
//Read the input file
getInput(fin, year, month, rain_in_month);

}
for (int i=0; i < MONTHS_IN_FILE; i++)
cout << year[i] << month[i] << rain_in_month[i] << endl;

//Close the input and output files
fin.close();
fout.close();
} // End function main

void OpenFile(ifstream& fin)
{
if (fin.fail())
{
cout<< "Input file opening failed.\n";
exit(1);
}
} // End function OpenFile

void OpenFile(ofstream& fout)
{
if (fout.fail())
{
cout<< "Output file opening failed.\n";
exit(1);
}
} // End function OpenFile

void getInput(ifstream& fin, int year[], int month[], int rain_in_month[])
{
int i = 0;
fin >> year[i] >> month[i] >> rain_in_month[i];
i++;
cout << year[i] << month[i] << rain_in_month[i] << endl;

return;

} // End function getInput
Oct 19 '06 #1
4 42322
tyreld
144 100+
Please don't double post.

What does your output look like?
Oct 19 '06 #2
I did not mean to double post some how it hit enter before I was done and I could not find the icon to delete one of them.

this is the output - minus some of the same lines for space:

-858993460-858993460-9.25596e+061
-858993460-858993460-9.25596e+061
-858993460-858993460-9.25596e+061
-200276
-858993460-858993460-9.25596e+061
-858993460-858993460-9.25596e+061
-858993460-858993460-9.25596e+061
Press any key to continue . . .
Oct 19 '06 #3
Your not passing your arrays in my reference.
Oct 19 '06 #4
//Process the records from the input file until the end of file is reached
while (!fin.eof())
{
//Read the input file
getInput(fin, year, month, rain_in_month);

}
...
...
...

void getInput(ifstream& fin, int year[], int month[], int rain_in_month[])
{
int i = 0;
fin >> year[i] >> month[i] >> rain_in_month[i];
i++;
cout << year[i] << month[i] << rain_in_month[i] << endl;

return;

}

This is probably not the problem you're having, but your getInput function within the while loop keeps initialize i to 0 so all your values are stored at (array)[0]. You then advance i by one to a value in your year, month, and rain_in_month arrays that is not defined and try to output it.

Try rewriting the code like this and see if it helps:
Expand|Select|Wrap|Line Numbers
  1. getInput(fin, year&, month&, rain_in_month&);
  2. ...
  3. ...
  4. ...
  5. void getInput(ifstream fin, int &year, int &month, int &rain_in_month)
  6. {
  7.     int i = 0;
  8.         while(!fin.eof())
  9.         {
  10.        fin >> &year[i] >> &month[i] >> &rain_in_month[i];
  11.        cout << year[i] << month[i] << rain_in_month[i] << endl;
  12.            i++;
  13.         }
  14.  
  15.     return;
  16.  
  17. }
I'm fairly new to C++ myself so I might have the pointers a bit screwed up. This way, you call getInput and it grabs all your data, assigns it to your arrays at the correct locations, and then outputs to the console what it saved.

- Miles
Oct 19 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Foxy Kav | last post: by
7 posts views Thread by John Dann | last post: by
29 posts views Thread by yourmycaffiene | last post: by
10 posts views Thread by nuke1872 | last post: by
6 posts views Thread by arne.muller | last post: by
7 posts views Thread by ianenis.tiryaki | last post: by
21 posts views Thread by Stephen.Schoenberger | last post: by
3 posts views Thread by jordanbondo | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.