Reading Data from a File

Edit: I have discovered that the errors I receive after the program reads my file are because it is trying to output data that is not there since the file does not read up to the maximum number of records (50). I need to figure out how to make it only output the number of records that were read onto the screen.


I'd appreciate any help. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	 //Defining limit of records
	 const int recordsLimit = 50;


	 //Defining new data structure for data entry
	 struct wave 
	 {
	 double year; 
	 double month; 
	 double day; 
	 double hour; 
	 double minute; 
	 double waveHeight; 
	 double waveLength; 
	 };

	 //Defining waveData array to hold 1-dimensional wave struct data
	 wave waveData[recordsLimit];

	 //defining data file for wave records using fstream and then opening it for output using function call
	 ifstream dataFile;
	 dataFile.open("wave.txt", ios::in);
	 
	 //Reading file - Includes error if not read
	 if (!dataFile) 
	 {
		 cout << "Unable to open file wave.txt";
	 }
	 else
	 {
	 //ifstream dataFile2;
	 //dataFile.open("steepWaves.txt", ios::in);

	 //while (dataFile >>  waveData[n].year >>  waveData[n].month >>  waveData[n].day >>  waveData[n].hour >>  waveData[n].minute >>  waveData[n].waveHeight >>  waveData[n].waveLength)
	 //Outputting info from file onto array



	 

	 while (! dataFile.eof())  // Read until we hit the end of file 
	{
			
		for (int n = 0; n <= recordsLimit; n++)
		{
		 dataFile >> waveData[n].year >> waveData[n].month >> waveData[n].day >> waveData[n].hour >> waveData[n].minute >> waveData[n].waveHeight >> waveData[n].waveLength;
		}
			
			
	}
	}

 //Outputting info from array onto screen
	 for (int n = 0; n <= recordsLimit; n++)
	 {
		 cout << waveData[n].year << waveData[n].month << waveData[n].day << waveData[n].hour << waveData[n].minute << waveData[n].waveHeight << waveData[n].waveLength << endl;
	 }


	dataFile.close();
	 }
Last edited on
i believe you should change line 50 to 59 to
1
2
3
4
5
6
7
int n = 0;
while (!dataFile.eof() && n <= recordsLimit)
{
dataFile >> waveData[n].year >> waveData[n].month >> waveData[n].day >> waveData[n].hour >> waveData[n].minute >> waveData[n].waveHeight >> waveData[n].waveLength;

n++
}
Alright. I implemented that into the reading portion of the file. But for lines 62 to 70. How do I make it so it only prints out the number of records that were read? I have no way of keeping a tab on that. I tried to create a variable such as numberOfRecords but that is inside the for while loop and c++ says it's an uninitialized variable.
Last edited on
The int variable n from before the while loop
int n = 0;

can represent the number of lines read...

so i guess you can

change lines 63 - 66 to
1
2
3
4
for (int i = 0; i <= n; i++)
	 {
cout << waveData[n].year << waveData[n].month << waveData[n].day << waveData[n].hour << waveData[n].minute << waveData[n].waveHeight << waveData[n].waveLength << endl;
	 }
Last edited on
Topic archived. No new replies allowed.