-2

For an assignment at school I have to make the game rogue. The way we load the rooms is from a file that looks like this:

10x12 de4 ds6 p9,8 g7,5 h1,1 M8,9
24x12 ds5 ds6 p9,8 g7,5 h1,1 M8,9
9x16 ds3 ds6 p9,8 g7,5 h1,1 M8,9
18x17 dn9 ds6 p9,8 g7,5 h1,1 M8,9
5x5 de4 ds6 p9,8 g7,5 h1,1 M8,9
16x11 dw7 ds6 p9,8 g7,5 h1,1 M8,9

Each room has a size to start then all the elements and their locations after it, separated by a space. When I read it in, I was unable to get each line (10x12 de4 ds6 p9,8 g7,5 h1,1 M8,9), I only got one element at a time (10x12 then de4 then ds6 and so on). Is there a way to read up until the end of the line and save that in a char ** rooms variable?

1 Answers1

3

First you have to know that at the end of each line there is a character '\n'. When you open a file, it create a cursor located at the start of the file. When you read one character, the cursor move on until it reach the end of the file, represented by the state code 'EOF' (-1 value).

To get one line, the strategy will be to read all character until you reach '\n'. And do it again, and again, until you reach 'EOF'. (be careful because if there is only one line, you will maybe not have '\n')

How to do it using code now:

Use the following function:

fopen() // To open the file

getline() // To read one line

Luckily for you these two function are enough, because they handle everything ('\n', 'EOF', cursor ...)

Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
  • 1
    Actually you are right It may be troublesome I'll change It. Looking at [this](http://stackoverflow.com/questions/12389518/representing-eof-in-c-code) – Orelsanpls Mar 04 '15 at 14:57