472,968 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,968 software developers and data experts.

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 42700
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

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

Similar topics

6
by: Foxy Kav | last post by:
Hi, another question from me again, i was just wondering if anyone could give me a quick example of reading data from a file then placing the data into an array for some manipulation then reading...
2
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
29
by: yourmycaffiene | last post by:
Okay, this if my first post so go easy on me plus I've only been using C for a couple of weeks. I'm working on a program currently that requires me to read data from a .dat file into a 2d array and...
10
by: nuke1872 | last post by:
Hello guys, I have a file names network.txt which contains a matrix. I want to read this matrix as store it as an array. I am new to stuff like these...can anybody help me out !! Thanks nuke
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
7
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) ...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
3
by: jordanbondo | last post by:
I have no clue why this isn't working. The top part that reads into size, shortTime, longTime, and threshold work fine. Down below where I try to do the exact same thing and read into x, i get...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.