fstream problem

closed account (N1qSE3v7)
Hi
I have problem my program doesn't want to save console in some output file and when I run it with this fstream code he doesn't stop to work when he should stop it.When I delete fstream code then it works...
Can someone help me...
Thank you

Last edited on
First off you have quite a bit of memory leaks in this program, which you need to take care of. Remember that whenever you use a new call there needs to be a matching delete call.

1
2
string *students = new string[STUDENT_NUMBER];
double *avr = new double[STUDENT_NUMBER];


So these will need to be deleted using the standard delete method for arrays. Though I am not sure why you are using dynamic allocation here since you are just using a static array anyways and would be much better served by declaring the arrays like this.

1
2
string students[STUDENT_NUMBER];
double avr[STUDENT_NUMBER];


Your next memory leak comes from your class.

1
2
3
4
5
6
student(int noSubject)
{
    subject = new string[noSubject];

    grade = new double[noSubject];
}


If you are going to declare these dynamically (Though I believe these would benefit from being std::vector but that is your choice), you need to delete their memory when you are done with it. Thankfully classes provide an easy means to do this through the destructor.

1
2
3
4
5
~student()
{
    delete[] subject;
    delete[] grade;
}


That will make sure that your arrays are deleted automatically whenever an instance of that class goes out of scope.

Now that the memory leaks are handled onto your main problem. I don't believe you fully grasp how std::ofstream works. You need to tell it what information you want to write to the file by passing it variables, you can't just pass in the main() function and expect it to work (Not to mention you should never be calling main() anyways).

There is a great tutorial on this site about writing and reading files through streams that you can check out to get more familiar with the subject http://www.cplusplus.com/doc/tutorial/files/ .

Otherwise here is a quick example.

Lets take your program and say that we want to write all the student's names and averages to the file. We would do something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
std::ofstream outputfile;
outputfile.open("Project1.txt");

// You always want to check if the file is open before writing anything to it.
if (outputfile.is_open())
{
    for (int i = 0; i != STUDENT_NUMBER; ++i)
    {
        outputfile << i << " - Student Name: " << students[i] << " Average: " << avr[i]  <<     "\n";
    }
}

outputfile.close();


Now you are probably thinking that this looks quite a bit like your std::cout loop at that bottom of your program. Well that is exactly the case, because std::cout is a stream just like std::ofstream is, the only difference is one is printing output to the console while the other is printing it to a text file. Generally they work exactly the same (With of course some differences), so it shouldn't be to hard to pickup file streams if you already know about printing and reading from the console.

Hopefully that helps a bit.
Topic archived. No new replies allowed.