# Probably a basic question

This topic is 2584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey there, At the moment I'm trying to create a simple roguelike game using the cmd (Not my choice, Our teacher told us to use it) Anyway, I'm having trouble with it. I was wondering if someone could tell me how to read the text in a text file, and output it to the cmd exactly as it's shown in the text file, and then, when a button is pressed (Say W) then I can change the players position on the map.
eg:
The program reads a text file and outputs
######
# #
#@ #
######
When the player types 'W' (Which I already know how to do, I'm using PdCurses)
The Current display is wiped off and replaced with
######
#@ #
# #
######

##### Share on other sites
In C, you can use functions such as fopen, fclose, fgetc, fread and fgets to open and read files. In C++, the std::ifstream class will do similar things.

If you're using another language further help could be provided, but note that it's frowned upon around here (quite rightly, IMHO) to give too much help with homework and assignments. If you were having an issue with the particulars of some specific thing such as the syntax of a looping construct or the correct use of a function, that would more likely be ok.

##### Share on other sites
<FONT color=#000000 size=2>Hey, Thanks for the help, I've managed to get fstream working as far as I know, in that it can read the file, but now I need to print it, the only problem being is that I need it to be in const char * format in order to print it to the screen. Any chance you guys could show me how to convert an fstream value into a const char * value? Thanks. Also, before I forget, heres the source code.</FONT>

##### Share on other sites

There are examples how to print text in a console.

##### Share on other sites
Why do you need it in a const char * format to print it? You haven't posted your code.

What you probably want to do is create an array representing the map, populate it once from a file and write that to the screen every "frame".

##### Share on other sites

Hey, Thanks for the help, I've managed to get fstream working as far as I know, in that it can read the file, but now I need to print it, the only problem being is that I need it to be in const char * format in order to print it to the screen.

The first program in any C++ tutorial will show you how to print characters to "the screen" via std::cout. Using std::cout, which is itself an ostream, you can write a multitude of objects to the console, including numerical primitives, std::strings and const char *s.

Any chance you guys could show me how to convert an fstream value into a const char * value?
[/quote]
This question doesn't really make sense. An fstream is an object that is used to read and/or write data between a file and C++ objects. Are you wanting to read data from an fstream in to one or more C++ objects, or write text-based representations of objects to an fstream?

##### Share on other sites
I need it as a const char* because I'm using PDCurses, and all variations of printw require the outputted statement to be in that format. I'm not using std::cout because as far as I knew, it could only print along one line, and without any options to place it physcially in the cmd, which I'll likely need, and it's the reason I'm using pdcurses. I'm not sure about the fstream question. When I tried to compile a version, visual studio 2010 replied with the error message along the lines of "error C2440: 'initializing' : cannot convert from 'std::fstream' to 'const char *' Heres the code <BR>#include "stdafx.h"<BR> #include<BR> <CURSES.H>#include <BR><STDLIB.H>#include<BR> <FSTREAM>#include <BR><IOSTREAM>using namespace std;<BR> static void main(int argc, char *argv[])<BR> { <BR>initscr(); <BR>fstream level; <BR>level.open ("level.txt" //I've placed the text file in the directory, so it can read it without adding C:\<BR> const char *p = level; <BR>printf(p);<BR> refresh(); <BR>getch();<BR> endwin();<BR> }

##### Share on other sites
The compiler is correct, a std::fstream isn't a const char *, and it knowns of no implicit conversion. You should use one of std::fstream's member functions to read the characters into a buffer (e.g. std::string or std::vector<char>).

I need it as a const char* because I'm using PDCurses, and all variations of printw require the outputted statement to be in that format.
[/quote]
That is OK. Remember you can get a const char pointer from a std::string by using its c_str() member function.

##### Share on other sites
Awesome, Thanks, It seems to be working so far, at least with cout, although I've got a problem.With at the moment I've got two iterations of the program, one using cout, and one where I'm attempting to use c_str. When I attempt to use cout, it outputs only a single line, even if I add a breakline that would normally be interpreted by the compiler and completely ignored like /n. I'm trying to work out c_str, but I don't know how to access it. How do I use it to convert the fstream value to string? If I try to call it with the argument level, it says that it's unidentified, even when I'm using fstream and string includes. Could you guys sheed some light on this? Thanks.

Here are the two different source codes, although Ones commented out because I'm too lazy to go through another new project and add all the different linkers and the like.

// reading a text file /*#include <iostream> #include <fstream> #include <string> #include <curses.h> using namespace std; int main () { string line; initscr(); ifstream myfile ("level.txt"); if (myfile.is_open()) { while ( myfile.good() ) { getline (myfile,line); cout << line << endl; system("pause"); //getch(); } myfile.close(); } else cout << "Unable to open file"; return 0; }*/ //#include "stdafx.h" #include <CURSES.H> #include <STDLIB.H> #include <FSTREAM> #include <IOSTREAM> #include <string> using namespace std; static void main(int argc, char *argv[]) { initscr(); fstream level; level.open ("level.txt"); c_str(level printf(p); refresh(); getch(); endwin(); } 

And heres the test level I'm using just in case you also needed it.
 #####/n # #/n # @ #/n # #/n #####/n 

1. 1
Rutin
24
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 9
• 46
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631749
• Total Posts
3002047
×