Need help with input/output files for arrays

I'm having problems getting the list of numbers from P4Data.txt to output into arrays in P4.txt.
The readFile function is correct but the writeFile function might not be.
Plus the coding within the main that will output to the file might not be right.

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
int main(){

  int a, size;
  T x[MAX_SIZE];	  // x-coordinate of data point
  T y[MAX_SIZE];	  // y-coordinate of data point
  T z[MAX_SIZE];	  // mass at data point
  int numDataPoints;
  
  ifstream fin; // declare ifstream object
  fin.open("P4Data.txt"); // attach file P4Data.txt

  if (fin.fail()){ // check it out
    cout << "\n\aBummer! File P4Data.txt"; // report bad news
    cout << " could not be opened. Goodbye.\n\n";
    exit(-1); // outta here
  }
  ofstream fout; // declare ofstream object
  fout.open("P4Out.txt"); // attach it to disk file
  
  readFile(x, y, z, cin);
  cout << readFile(x, y, z, cin) << "\n\n";
  writeFile(x, y, z, numDataPoints, cout);
  fout << writeFile(x, y, z, numDataPoints, cout); //ERROR WITH THIS LINE
  
  cout << "\nfin and fout both opened.\n\n"; // temp output
  fin.close(); // close up files when done
  fout.close();

  system("Pause");
  return EXIT_SUCCESS;
}

//*************************************************************************
//*************************************************************************

int readFile(T x[], T y[], T m[], istream & iStream){


  int numDataPoints; // temp value

  iStream >> numDataPoints;
  if (numDataPoints > MAX_SIZE){                  // ensure not too big
    cout << "\n\n\aTruncating data ";
    cout << "due to size constraints.\n\n";
    numDataPoints = MAX_SIZE;                     // truncate if needed
  }
  for (int i = 0; i < numDataPoints; ++i) {       // read data groups
    iStream >> x[i];
    iStream >> y[i];
    iStream >> m[i];
  }

return numDataPoints;
}

//*************************************************************************
//*************************************************************************

void writeFile(T x[], T y[], T m[], int numDataPoints, ostream & oStream){

  oStream << numDataPoints;
  if (numDataPoints > MAX_SIZE){                       // ensure not too big
    cout << "\n\n\aTruncating data ";
    cout << "due to size constraints.\n\n";
    numDataPoints = MAX_SIZE;                          // truncate if needed
  }
  for (int i = 0; i < numDataPoints; ++i) {            // read data groups
    oStream << x[i];
    oStream << y[i];
    oStream << m[i];
  }
}
Last edited on
The output should be in this form:


index    x[index]   y[index]   m[index]
0          0.00           0.00       12.34
1          0.00         10.00         7.77
2          0.11         11.10         1.11
3          1.88           8.90         1.11
4          2.00         10.00         2.34
5          4.00           8.00         3.14


Except lined up nicely
Topic archived. No new replies allowed.