trying to fstream...


I am trying to make a simple program to extract some text from a file and output it in a console box. I am using the DevC++ compiler and the following code:

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

void titleopen ()
{
char title [20];

ifsream titlebox ("titlebox.txt");
getline (cin,title);
titlebox.close;

cout >> title >> "Is announcing:" >> endl;


return (0);
}


void messageopen()
{
char message [300];

ifstream messagebox ("messagebox.txt");
getline (cin,message);
messagebox.close;

cout << message << endl;

return (0);
}

int main()

char yesno;
{
title ();
message ();
cout << "press Y to exit" << endl;
cin >> yesno;


if (yesno == y)
{
cout << "good bye" << endl;
break;
}
else
{
cout << "You did not press Y" << endl;
}

return (0);

}


When I try to compile it, the compiler keeps telling me that ifstream is not a recognized function. It has also given me repeated 'parse errors' but as of now, it stops at ifstream, and says that it has been confused by earlier errors (though it only lists the one) and that it is bailing out... If you could plz help me out a bit... TY!!!
This code is from MSVisual C++ 6.0, but I hope it will work as well in DevC++ (it should:-)).

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>
#include <string>
using namespace std;
bool readfile (string filename)
{
	char buffer [513];
	ifstream fin(filename.begin());
	if (!fin.is_open())
		return false;
	fin.getline(buffer, 512, '\n');
	cout << buffer << endl;
	fin.close();
	return true;
}

int main ()
{
	do
	{
		readfile ("title.txt");
		readfile ("messagebox.txt");
		cout << "Do you want to start again? y/n: ";
	}
	while (cin.get() == 'y');
	return 0;
}


readfile function reads only one line from a file, like your functions does, but you can surely modified it if you need:-)
Last edited on
Also, you spelled "ifstream" wrong:

ifsream titlebox ("titlebox.txt");
lol TY now I feel like a doofus but hopefully that will work!
Consider this part of your code:

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
void titleopen ()
{
char title [20];

ifsream titlebox ("titlebox.txt");
getline (cin,title);
titlebox.close;

cout >> title >> "Is announcing:" >> endl;


return (0);
}


void messageopen()
{
char message [300];

ifstream messagebox ("messagebox.txt");
getline (cin,message);
messagebox.close;

cout << message << endl;

return (0);
}

1. In both functions you are trying to close the files after reading from them. To close a file, you call the close() function, for example:

filename.close();

You forgot to put the () after close in both places. close is a function call, it needs ().

2. Your both functions are of type void. They don't return anything. But still you have included return(0) at the end of both functions. Remove return(0) from both.

Taking another part of your code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()

char yesno;
{
title ();
message ();
cout << "press Y to exit" << endl;
cin >> yesno;


if (yesno == y)
{
cout << "good bye" << endl;
break;
}
else
{
cout << "You did not press Y" << endl;
}

return (0);

}

1. I can't see the { after your main()
2. if (yesno == y) tries to compare the contents of the char variable yesno with y. y should be a char constant. Then you should put it as 'y'
3. break is used to exit a loop. I can't see a loop in your code. break is meaningless here. You need to remove it.

Check these and then reply if it compiles and works as expected. And finally a general note: style is essential to make your code readable. Please use indentation.

Last edited on
Topic archived. No new replies allowed.