Reading file errors

I have been looking around and have found a couple of examples on how to do this, but none of them have worked.

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <fstream>
#include <limits>
using namespace std;
using namespace sf;

//Declarations

//Global variables
const int scl = 8;         //the images will be placed vector2f(image[][].x * scl, image[][].y * scl)
Sprite Bomberman;
Sprite Bomb;
//Classes

//Structures
struct block{
    bool isBroken;
    Sprite stone;
};
struct maps{
    int blockCount;
    int width;
    int height;

};
//Functions
ifstream& GotoLine(std::ifstream& file, unsigned int num){
    file.seekg(std::ios::beg);
    for(int i=0; i < num - 1; ++i){
        file.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    }
    return file;
}
maps getMapData(string fileName){
    maps rtrn;
    ////////////////////////////////
    ///Line 1 = comment
    ///Line 2 = comment
    ///Line 3 = int width
    ///Line 4 = comment
    ///Line 5 = int height
    ///Line 6 = Dirt and wall images
    ///Line 7 = Entities images
    ///Line 8 = Dirt array
    ///Line 9 = Entities array
    ///Line 10 = Walls array
    ///////////////////////////////
    string path = ("/DATA/mapData/" + fileName);
    //////////////////////////////
    ifstream& mapFl(char(path));
    string readline = "";
    int line_no = 0;
    GotoLine(mapFl, 3);
    mapFl >> readline;
    int width_ = atoi(readline.c_str());
    while (line_no != 5 && getline(mapFl, readline)) {
        ++line_no;
    }
    int height_ = atoi(readline.c_str());
    while (line_no != 6 && getline(mapFl, readline)) {
        ++line_no;
    }
    string idlePath = readline;
    while (line_no != 7 && getline(mapFl, readline)) {
        ++line_no;
    }
    string ePath = readline;
    while (line_no != 8 && getline(mapFl, readline)) {
        ++line_no;
    }
    string dirtAr = readline;
    while (line_no != 9 && getline(mapFl, readline)) {
        ++line_no;
    }
    string entityAr = readline;
    while (line_no != 10 && getline(mapFl, readline)) {
        ++line_no;
    }
    string wallAr = readline;
    ////////////////////////////////

}
//More Global variables

//Main
int main()
{
    cout << "Hello world!" << endl;
    return 0;
}

These are the contents of the map file.

---------------------- 2 IMAGES, 3 ARRAYS -----------------------
//width
20
//height
20
/DATA/images/Map1/DIRT_WALL.png
/DATA/images/Map1/ENTITIES.png
/DATA/Arrays/Lvl1/DIRT.array
/DATA/Arrays/Lvl1/ENTITIES.array
/DATA/Arrays/Lvl1/WALLS_1.array

(Contents of the map file) //Btw, this file is located and named: /DATA/mapData/Lvl1.map
With the directory coming before /DATA/ being the project directory.
The problem is that it doesnt want to compile and it keeps giving me errors relating to either Gotoline when i use that method, or getline, when i use that method. Thanks!
Thanks for the help!
Last edited on
You are a victim of the most vexing parse. https://en.wikipedia.org/wiki/Most_vexing_parse

The problem is that the compiler sees this line ifstream& mapFl(char(path)); as a function declaration of a function named mapFl that takes a char as argument and returns a ifstream&.

You should change the line to ifstream mapFl(path);
Last edited on
Ok, i did that, and followed the wikipedia page, but i came up with the error that path had to be a char, or so i understood, so then i went through converting a string into a char, until i successfully converted, but it then gave me an error:
1
2
3
4
5
6
7
8
9
10
||=== Build: Debug in Bomberman (compiler: GNU GCC Compiler) ===|
E:\C++ Projects\Bomberman\main.cpp||In function 'std::ifstream& GotoLine(std::ifstream&, unsigned int)':|
E:\C++ Projects\Bomberman\main.cpp|35|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|
E:\C++ Projects\Bomberman\main.cpp||In function 'maps getMapData(std::string)':|
E:\C++ Projects\Bomberman\main.cpp|61|error: invalid initialization of reference of type 'std::ifstream& {aka std::basic_ifstream<char>&}' from expression of type 'char'|
E:\C++ Projects\Bomberman\main.cpp|41|warning: unused variable 'rtrn' [-Wunused-variable]|
E:\C++ Projects\Bomberman\main.cpp|66|warning: unused variable 'width_' [-Wunused-variable]|
E:\C++ Projects\Bomberman\main.cpp|70|warning: unused variable 'height_' [-Wunused-variable]|
E:\C++ Projects\Bomberman\main.cpp|93|warning: no return statement in function returning non-void [-Wreturn-type]|
||=== Build failed: 1 error(s), 5 warning(s) (0 minute(s), 0 second(s)) ===|

and this is my code, sorry for the mess
pastebin.com/qgsnZNiS
This line
 
    ifstream& mapFl(*cstr);


should be (C++11 or later)
 
    ifstream mapFl(path);


or (prior to C++11)
 
    ifstream mapFl(path.c_str());

Ok, I fixed the code, it will compile and all, it will run, but now i made a cout statement that outputs everything i got, and it isn't working, it just prints 2 0s,
Here is my new code:

http://pastebin.com/pJZpgMpt
it isn't working, it just prints 2 0s

It's a good idea to check that the file was actually opened.
54
55
56
57
58
59
    string path = ("/DATA/mapData/" + fileName);
    ifstream mapFl(path.c_str());
    if (!mapFl)
    {
        cout << "Error: could not open file " << path << '\n';
    }
Last edited on
Topic archived. No new replies allowed.