Text file with Parallel Arrays

I'm trying to read a file that then sorts three sets of data (states, miles, shelters) into three parallel arrays. I've compiled it, but I'm still getting some error messages. Greatly appreciate an extra pair of eyes or two on this!

Cheers!

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
 #include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>

using namespace std;

// Declare maximum of states
const int MAX_STATES = 14;

// Arrays to be used by entire program

string initials[MAX_STATES];
double miles[MAX_STATES];
int shelters[MAX_STATES];

//Functions
int readData(int size);

void displayAll (int count);

void main ()
{
        cout << readData(MAX_STATES);
        displayAll(readData(MAX_STATES));

        ofstream outFile;
        outFile.open (inputfilename);
        outFile.close();
}

// Function: readData
// Description: Reads the data and stores in parallel arrays
// Parameters: int size
//  Returns: int  count


int readData(int size)

{

 cout << "Enter Appalachian Trail file name: " << endl;
 ifstream in_stream;
 string inputfilename;

 cin >> inputfilename;

// Error check if input file exists

 in_stream.open(inputfilename);
   if (in_stream.fail())
        {
                cout << "Input file does not exist. \n";
                exit(1);
        }
// Error check inpute file empty

   ifstream in(inputfilename);
   if (in.is_open())
   {
    in.seekp(0,ios::end);
    size_t size = in.tellg();
    if (size == 0)
      {
        cout<< "Input file is empty. \n" ;
      }


  in_stream.open(inputfilename.c_str());
  int count = 0;

  while (!in_stream.eof())
  {
        for ( i; i < count; i ++)
        {
                in_stream >> initials[i] >> miles[i] >> shelters[i];
        }
 }

  return count;

  in_stream.close();

}

void displayAll(int count)
{

        for (int i = 0; i <= count; i++)
                cout << initial[i] << miles[i] << shelters[i] << endl;

}
Last edited on
when you put your prototypes at the top, don't declare the variables. What this will do is require you to pass an argument, causing compiler errors if you don't pass an arg. It's good practice.

You can eliminate in_stream and just use in as the alias of your stream.

Also, in.seekg(0, ifstream::end) jumps the pos to the end of the file. You might want to fix that. Opening the file starts it at the beginning. You can use ifstream::pos_type (which is a typename) and declare a variable to store the first pos in. You can then seek the end, and then use in.seekg(pos) to go back to the beginning (once you have calculated the bytes of the file by using both positions).

Line 73: using in.good() will return false if the file stream is bad, or the end of the file is reached, whereas .eof() returns true only if the end has been reached. If somthing should happen, and the file stream becomes corrupted, then it will create an infinite loop.

line 60: you can only open 1 stream at a time, for each file; it's also safer...

****************************
all-in-all: you should declare only 1 ifstream, and open/close it as you wish. Other than that, I highly recommend making your code easier to read by spacing the curly-braces properly.
I have everything compiling now except one error message:

'struct std::ifstream' has no member named 'seekp'




in_stream.open(inputfilename.c_str());
 if (in_stream.is_open())
 {
    in_stream.seekp(0,ios::end);
    size_t size = in_stream.tellg();
    if (size == 0)
    {
        cout<< "Input file is empty. \n" ;
    }
 }


can't seem to figure out what's going on with that
seekg, not seekp.

seekp() is a member of ofstream.

The way I remember:

seekp() = seekput, put is used to output

seekg() = seekget, get is used to get input.

ALSO: in.seekg(0, ifstream::end), not in.seekg(0, ios::end);.
Last edited on
Topic archived. No new replies allowed.