Need help with outputting integers as arrays to a file

I can't get the list of numbers from my input file to output as a list of arrays in the output file (nothing even gets outputted to the file)


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
95
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;
const int MAX_SIZE = 200;

typedef long double T;	// data type for project

int readFile(T x[], T y[], T m[], istream & iStream = cin);
void writeFile(T x[], T y[], T m[], int numDataPoints, ostream & oStream = cout);

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

//include proj4.h
//include readFile.h
//include writeFile.h

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);
  
  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 << i;
    oStream << x[i];
    oStream << y[i];
    oStream << m[i];
  }
}
Last edited on
At least instead of these two statements

1
2
 readFile(x, y, z, cin);  //TAKE OUT AFTER SPLITTING
 cout << readFile(x, y, z, cin) << "\n\n";


you should write

1
2
 numDataPoints = readFile(x, y, z, cin);  //TAKE OUT AFTER SPLITTING
 cout << numDataPoints << "\n\n";


Also why do you use cin instead of fin in the call of the function?

Also take into account that you defined arrays of long double not int
Last edited on
That's how the explanation was in the directions fro the project. Also I tried replacing those lines of code and still there's no output to the P4Out.txt file
You do not write to the file. You write to cout. Also check what is the value of numDataPoints the statement

cout << numDataPoints << "\n\n";

outputs.
I'm not trying to output to the screen. I'm trying to read in a list of numbers from P4Data.txt (hence the readFile function) and then output these numbers in the form of arrays in P4Out.txt (hence the writeFile function)
I trust your code. You are reading from the standard input and writing to the standard output.

Look through your program. I already have pointed out that you are reading from the standard input.

Try to change

numDataPoints = readFile(x, y, z, cin); //TAKE OUT AFTER SPLITTING

to

numDataPoints = readFile(x, y, z, fin); //TAKE OUT AFTER SPLITTING

and

writeFile(x, y, z, numDataPoints, cout);

to

writeFile(x, y, z, numDataPoints, fout);
Last edited on
Because I'm using cin and cout rather than fin and fout?

My professor gave us most of the code and the only thing we needed to code ourselves was the writeFile function and to add whatever is needed in the main to perform the purpose of writeFile
Changing those to fin and fout gives me errors saying that fin and fout need to be declared. I'm assuming it's because they aren't in the libraries I have included.
They are declared. I wonder do you look your own code?!
Show what is your resulted code.
//
Last edited on
You had not to change this declarations

int readFile(T x[], T y[], T m[], istream & iStream = fin);
void writeFile(T x[], T y[], T m[], int numDataPoints, ostream & oStream = fout);

I did not ask you to do this. So use their original declarations.
Oh I see now. I was confused and not sure why I had to do that. Now I'm actually getting something in my output file but it's only the first integer from the input file. I need to figure out how to make it in the form I had at the bottom of my original post
My professor gave us most of the code and the only thing we needed to code ourselves was the writeFile function and to add whatever is needed in the main to perform the purpose of writeFile


Could you post the unaltered version your professor gave you?
I don't have it anymore
I did not ask you to change this statement

fout << numDataPoints << "\n\n";

It shall look like as

cout << numDataPoints << "\n\n";


After you will change it say what is the vvalue of numDataPoints ?
cout writes to the screen in the terminal whereas fout writes to the file P4Out.txt (which is what I want).

I don't understand what you're asking at the last part of your comment.
Last edited on
So I just added these lines of code in the main

1
2
  writeFile(x, y, z, numDataPoints, cout);
  cout << numDataPoints;


and took out the lines about readFile and I get an array output.
The only problem is that the first number and last 3 numbers are missing from the output.
Last edited on
It is difficult to deal with you.

Why are you doing what I did not ask?!!!
Because what you were telling me was not working. What I have now is 99% right except it's missing a couple numbers from the input file. This might be due to a looping problem.
You helped me realize some things, so I figured it out on my own. I made some adjustments to the writeFile function and included that code in the main I posted a little bit ago. It works now.
Last edited on
Topic archived. No new replies allowed.