How to clear a previous file to open a new one

Apr 19, 2010 at 11:57pm
Hey, I'm pretty much almost done with this program, but I'm not sure how to clear a previous file (after opening it) in order to open a new one. My program is supposed to open a file with a maximum of 100 unordered decimals and allow the user to sort and/or search, or open a new file.

Here's part of the program:

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
// This program is supposed to read a file with a maximum 
// of 100 unordered decimal numbers and allows the user
// to sort the numbers and/or do a search.

# include <iostream>
# include <fstream>
# include <string>

using namespace std;

// Prototypes

void Instrucciones(void);
int menu(void);
bool CheckFile(string filename);
int ReadFile();
bool BubbleSort(float [], int);
bool SelectionSort(float [], int);
void LinearSearch(float [], float, float);
void BinarySearch(float [], int, float);

int main()
{
  // Declaring variables
  int opcion = 0;  
  ifstream ListNumbers;
  string filename;
  int size = 0;
  float array[100];
  float numbers;
  float value;
  bool order;

// This while is so the program will keep looping as long as the option
// chosen is between 1-5.
  while (opcion >= 1 || opcion <= 6){
        
  // Display instructions
  Instrucciones();
  
  // Display menu
  opcion = menu();

  // Displays the option chosen by the user
  cout << "You've chosen option: " << opcion << endl << endl;

  if (opcion == 1){                    
     cout << "Enter the name of the file: " << endl;
     cin >> filename;
     
     if (CheckFile(filename)==true){
        ListNumbers.open(filename.c_str(),fstream::in);
        
        while (!ListNumbers.eof()){
              // Keeps reading the file until it ends
              ListNumbers >> numbers;
              if (ListNumbers){
                 array[size] = numbers;
                 //ListNumbers >> numbers;
                 cout << "The numbers are: " << array[size] << endl;
                 size++;
              }
             
        }
        cout << "The amount of numbers in the file is: " << size << endl;
        cout << "The file " << filename << " exists. " << endl;
        
        order = false;
        ListNumbers.close();
        
     }
     else {
         cout << "The file doesn't exist." << endl;
     }
    
}


Any help or hints would be appreciated, thanks.
Apr 20, 2010 at 12:00am
After closing it, you also might also have to clear it (using .clear()).

Btw, I think you are missing some ending }
Apr 20, 2010 at 12:31am
Allright, well I tried the .clear() command, and it somewhat works, unfortunately, when i open the new file (after opening a previous one), this time it shows the numbers from the new file, but it also shows/keeps the numbers from the previous file, and keep in mind that this isn't the whole program. If you want me to post it completely, let me know.

Thanks for your time and help. :)
Apr 20, 2010 at 12:37am
Are you clearing your array of floats and resetting all your counter variables? (like size)
Apr 20, 2010 at 1:00am
No, that's the problem, I'm not quite sure how to do it. Would I have to use something like delete [] array? And as for the resetting of counter variables, well I am unsure of where to reset it.
Apr 20, 2010 at 1:15am
I tried resetting the size variable by inserting size = 0; right before I closed ListNumbers and option 1 works fine, but when I go to do other options they won't work. Any suggestions?
Topic archived. No new replies allowed.