I/O advice request

Hello everybody, I'm working on a program that reads from a file a sequence of numbers and push them into a stack. I give the file name that containn the numbers manually into a function of the stack. What if I want to let the program read the file name from the keyboard in the main function? Does exist a method to do it? Thanks for your time, I leave the code of my program below.

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
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class stack {
	int max;
	int top;
	int* items;	
	
	public:
		
		stack(int size);
		void autopush();
		~stack();
		void push(int c);
		void pop();
		void showlist();
};

stack::stack(int size){
	
	max = size;
	top = 0;
	items = new int[max];
}
void stack::autopush(){	
	
		ifstream myfile;
		myfile.open("nums.txt", ios::in);
		
		while (top < 5) {
			myfile >> items[top];
			top++;
		}
		
}

stack::~stack(){
	
	delete[] items;
}

void stack::push(int c){

	items[top] = c;
	top++;	
}

void stack::pop(){
	
	top--;
}

void stack::showlist(){
	
	int lect;
	lect = 0;
	
	while (lect != top){
		
		cout << items[lect] << endl;
		lect++;
	}
}


int main(){
	
	stack st1(5);
	
	st1.autopush(filename);
	st1.showlist();
}


The file of numbers (nums.txt) is written like this:

1 233 4234 6563 1231

The numbers, obviously can change and are ints just for this case.

Thanks again, linkcraig.
Last edited on
There's a really easy way to handle this sort of problem. When designing your class, don't write methods that "do the work." Write methods that "make doing the work easy." If you think like this, you'll write code that is much more flexible.

In this case, you have a method called autopush() that pushes items from the file "nums.txt" onto the stack. That's useful for one thing and one thing only. It would be much more useful and flexible if autopush() pushed items from an existing istream:
1
2
3
4
5
6
void stack::autopush(istream &is) {
   int num;
   while (is >> num) {
       push(num);
   }
}

This is slightly different from what you have because it reads all items from the file, but that's probably what you want.

Now in main you do
1
2
ifstream strm("nums.txt");
st1.autopush(strm);

Notice that this doesn't even involve more code. It just moves the stream to a different place. But look what you can do now:
1
2
3
4
5
6
// push the numbers from cin:
stk.autopush(cin);

// push a bunch of nums
istreamstream ss("2 4 6 8 10 12");
stk.autopush(ss);


BTW, you should add error checking code to push() to deal with the case where the stack is full. Either create more space or don't push the item.


Hi dhayden! Thanks for your answer. What you wrote was really useful for me! Thanks! But I wanted to know if its possible to write from keyboard the name of the file that has to be red.

For example:

The program asks the name of the file, then try to open it, is it possible?

Anyway i changed my code like this:

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
 #include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class stack {
	int max;
	int top;
	int* items;	
	
	public:
		
		stack(int size);
		void autopush(istream &is);
		~stack();
		void push(int c);
		void pop();
		void showlist();
};

stack::stack(int size){
	
	max = size;
	top = 0;
	items = new int[max];
}
void stack::autopush(istream &is){	
	
	int num;
	
		while(is >> num){
			push(num);	
		}
}

stack::~stack(){
	
	delete[] items;
}

void stack::push(int c){

	items[top] = c;
	top++;	
}

void stack::pop(){
	
	top--;
}

void stack::showlist(){
	
	int lect;
	lect = 0;
	
	while (lect != top){
		
		cout << items[lect] << endl;
		lect++;
	}
}


int main(){
	
	int maxV;
	fstream myfile("nums.txt");

	myfile >> maxV;
	
	cout << maxV;
	stack st1(maxV);
	
	myfile.seekg(2);
	st1.autopush(myfile);
	
	st1.showlist();
} 


And the file content is this:

10 2 32 39 123 89 83 2 1 234 5

the output is this one:

2
32
39
123
89
83
2
1
234
5


Exactly what i wanted! Thanks again, linkcraig.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int
main()
{
    string fileName;
    int maxV;

    cout << "Please enter the file name: ";
    getline(cin, fileName);

    fstream myfile(fileName);

    myfile >> maxV;

    cout << "max stack size is " << maxV << "\n\n";
    stack st1(maxV);

    st1.autopush(myfile);
    st1.showlist();
}

Thanks again, but it gives me this error:

[Error] no matching function for call to 'std::basic_fstream<char>::basic_fstream(std::string&)' 


Is this means I need a different included library?

I used these:

1
2
3
4
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string> 


Thank you again!
Thanks again, but it gives me this error:

You must be using an older version of your compiler. Update it when you get the chance. Until then, you can use fileName.c_str().
Topic archived. No new replies allowed.