Problem Printing to An Output File From A Function

In my program, each time a function is called it copies over everything that is already in the output file. If I only use one function, I get the correct output, but if I use both functions it only shows the most recent function used in the out put file. The functions that print to the output file are Read_In and List_Sort.


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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//James.txt 19 32 45 25 65 15 39 44 33 19 92 54 62 23 21 41 11 22 72 13 19 18 43 37 36 99 -1

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

int main ( )
{
	fstream dataFileI ("James.txt", ios::in);
	fstream dataFileO ("JamesOut.txt", ios::out);
	if(!dataFileI)
		cout << "Error in opening the input file.\n";
	if(!dataFileO)
		cout << "Error in opening the output file.\n";
	void Read_In (int List[ ]); // function will read in the input file and put it into an array
	int H_L_S_James (int& highest , int& lowest, int List[ ]); // function to find highest, lowest, and sum of array
	void List_Sort ( int List[ ] , int Size); //Function will sort array in descending order
	int Linear_Search (int Search , int List [ ] , int Size); //Function for the linear search
	int /*token*/ Search , Position_Of_Search;
	int List[26];
	int List_Sum , highest = 0 , lowest = 100 , Size = 26;
	/*dataFileO << "The Original List" << endl
			  << "=================" << endl;
	for (int i = 0; i < 26 ; i++) //the following will print the original list
	{
		dataFileI >> token;
		List [i] = token;
		dataFileO << "  " << i + 1 << "      " << List[i] * 1000 << endl;
	}*/
	Read_In (List);
	List_Sum = H_L_S_James (highest , lowest , List);
	cout << List_Sum << endl;
	List_Sort (List , Size);
	cout << "What number would you like to search for?\n";
	cin >> Search ;
	Position_Of_Search = Linear_Search ( Search , List , Size);
	if (Position_Of_Search > -1)
		cout << "That is found in position " << Position_Of_Search + 1 << endl;
	else
		cout << "That number was not found in the list\n";
	return (0);
}

//Following function will read in the input file and fill an array
void Read_In (int List[ ])
{
	int token;
	fstream dataFileI ("James.txt", ios::in);
	fstream dataFileO ("JamesOut.txt", ios::out);
	dataFileO << "The Original List" << endl
			  << "=================" << endl;
	for (int i = 0; i < 26 ; i++)
	{
		dataFileI >> token;
		List [i] = token;
		dataFileO << "  " << i + 1 << "      " << List[i] * 1000 << endl;
	}
}

/* This function will find the highest and lowest value in the array and also calculate the sum of all the entries
   It will return the value of the sum */
int H_L_S_James (int& highest , int& lowest, int List[ ])
{
	int SUM = 0 ;
	for ( int i = 0; i < 26; i++)
	{ 
		if (List[i] > highest)
			highest = List[i];
		if (List[i] < lowest)
			lowest = List[i];
		SUM = SUM + List[i] ;
	}
	return SUM ;
}

void List_Sort ( int List[ ] , int Size)
{
	fstream dataFileO ("JamesOut.txt", ios::out);
	int largest , largest_location , temp;
	for (int pass = 1 ; pass <= Size; pass++) // this loop will sort the list into ascending order
	{
		largest = List[pass-1];
		for (int k = pass ; k <= Size-1 ; k++)
			if (List[k] > largest)
			{
				largest = List[k] ;
				largest_location = k ;
			}
			if (largest != List [pass-1])
			{
				temp = List[pass-1] ;
				List[pass-1] = largest ;
				List [largest_location] = temp ;
			}
			dataFileO << "   " << pass << "          " << List[pass-1] << endl;
	}

}

// This will perform a linear search on the array
//If the number is not found it will return the value -1
//If it is found then it will return the index number of the number
int Linear_Search ( int Search , int List [ ] , int Size)
{
	for (int i = 0; i < Size ; i++)
	{
		if (List[i] == Search)
			return i;
	}
	return -1;
}

[code]
[/code]
Last edited on
friends don't let friends not use
[code][/code]tags


use code tags

it's the first button under the Format: menu

honestly I would like to help you but I won't look at unformatted code it hurts
Last edited on
There, i used the code tags. Sorry, i didn't know about that, never posted before. Please help!
Your problem is because you are closing and reopening the file(s) in each function.

If you want to keep the same file open, it has to be open in a broader scope and passed to the function as a parameter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void DoSomethingWithAFile(ostream& f)
{
  // do stuff with 'f' here
}

void DoSomethingElseWithAFile(ostream& f)
{
  // do other stuff with 'f' here
}

int main()
{
  ofstream file("myfile.txt");  // open the file only once

  DoSomethingWithAFile( file ); // then pass that file to your functions
  DoSomethingElseWithAFile( file );
}
I got it to work using your tip. Thank you very much! I was thinking that I had to use it as a parameter, but I wasn't sure how to do that. Thanks again
Topic archived. No new replies allowed.