Writing an array to an outfile

Pages: 12
Please bear with me as I have to be honest and say this isn't my best subject and I'm struggling with some aspect of it.

What I need to do is to get test scores write to a file and then in another program write a file to sort and group the scores within a range. Pretty comfortable with the second part, what I'm having difficulties with is the first part, getting and writing the scores to an file. The file does exist, but there is no data in it when I enter it.

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
#include <iostream>
#include<fstream>
using namespace std;

int main()
{
int score[26];
int counter;



for (counter = 0; counter < 26; counter ++)
{
   ofstream scoreFile;
   scoreFile.open("scores.out");
	cout << "Enter test score to store   ";
	cin >> score[counter];
	scoreFile << score[counter];

	
	cout << endl;
	scoreFile.close ();
}

    
return 0;
}
Anyone?
so, in the first file you are storing inputted scores and in the other you are reading them in and displaying them on screen?

move your scoreFile.close() to the out side of the loop.

--also your ofstream scoreFile; and scoreFile.open();
Last edited on
Awesome, Thanks Kyle. I wasn't sure if it should have been in or out of the loop.

Now it does record the array numbers, but they're all together with no spaces. Will the next program that needs to read them be able to tell the elements apart?
change cout << endl;
to
scoreFile << endl;

see if that helps

**if you just want everything on one line then do a scoreFile << " "; instead
Last edited on
the cout << endl; had been removed when I was trying some different things.

Putting it back in fixed the issue. Thanks again, looks Like I can start the 2nd part. Big props.

I really want to write this whole thing by myself as much as I can. Other programs we have done, while i have done the work, I have also looked at the programs which are listed all over the internet, so I want this to be entirely my own.
Last edited on
thanks anything else?
As long as I have the syntax down for reading the information back into the next part, I should be good.
O.K. Need advice again. Think this is correct way to open it, not 100% sure though. What I'm lacking now is how to access the array's information. When I do what I did below it displays the contents of some other file. Also the file is .out, so is that correct format ("scores.out")? What I'm trying to do is to make sure that I'm pulling the right information so that I can then put in the loop and manipulate it the way I need to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdlib>
#include <iostream>
#include<fstream>




using namespace std;

int main()
{
ifstream inFile;
inFile.open("scores.out");
cout << "               " << inFile; 
system("pause");
    inFile.close();
    return 0;
}
"scores.out" even a valid file type? use .txt instead for both
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}


this is from:

http://cplusplus.com/doc/tutorial/files/

try to follow this example fi you can
That worked well. Nice. Wish I could send ya a six pack or something! Thanks.
i dont drink,lol anything else or is that all?
Nah, just have to figure how to compare the information to manipulate it, but that's in the book and on the web so I really should be able to do that part.
Thanks again.
Just a quick question.

If I use a while != EOF type statement, does it automatically recognize the EOF? Or should I use a marker like -999 and extend the array by one element and add -999?
you can use while(!scoreFile.eof()) if you want, dont be surprised if something wierd happens because i get trouble some times but you should be ok
Last edited on
OK now I'm getting frustrated, and that's not good. :) I'm trying to get it to read the values with a loops, but it's only reading the first value. Honest, this is last thing I'll ask for help with! :)
Below is basically what I'm trying to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;
int i;
int main(int argc, char *argv[])
{
    ifstream infile;
    infile.open("scores.txt");
    for (i=0; i < 26; i++);
    int score;
    infile >> score;
    cout << score;
    system("PAUSE");
    return EXIT_SUCCESS;
}
Actually pretty sure I got it. I moved from dev (bloodshed) to visual c++ (2010)

only issue now is how do I get visual to simulate a run? LOL It was easy in bloodshed, all I can find is compile in visual.
If I recall correctly, Ctrl-F5 (or was it just F5?) runs a program in debug mode. Else, there should be a green arrow to run the program. C++ wasn't meant to be interpreted by anything else than a compiler, so... :/

-Albatross
Last edited on
albatross is right. there should be a little "play" button in the top toolbars.
Pages: 12