473,473 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C++ reading data from file into array

2 New Member
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 42729
tyreld
144 New Member
Please don't double post.

What does your output look like?
Oct 19 '06 #2
tlcvetan158
2 New Member
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
rahvege
1 New Member
Your not passing your arrays in my reference.
Oct 19 '06 #4
iknc4miles
32 New Member
//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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.