Thread: Filestreams - opening them and peek()

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    UK
    Posts
    2

    Filestreams - opening them and peek()

    Hey there.

    After several hours fiddling with my latest bit of code, I've finally decided to admit defeat and seek reinforcements, so to speak. There're two problems, both related to my filestreaming, and I'd be very grateful if someone could give me a few pointers (er, advice, that is) in the right direction.

    Just so you know, I'm more or less a beginning programmer, using Dev C++, and running Windows XP Home Edition. The code will eventually be a small text-based RPG (I've made one before but it was quite badly-coded; this is 2.0 ).

    --------------------------------------------------------------

    Problem One:

    Code:
    ifstream MapLoad(player.GetLocation());
    From that line of code (I think that part is all that this particular problem concerns, and the whole program-in-progress is spread over several files anyway), I'm getting an error:

    no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string)'

    player.GetLocation() returns a string, so I was hoping that the above code would try to open a file with the same name as the value returned by player.GetLocation() for writing to.

    I've tried getting the value that player.GetLocation() outputs and saving it to a variable, then putting that in instead, but that didn't work, either. Nor did declaring the filestream (is that the right phrase?) and then opening it separately. I found that putting a plain string in in quotes works, but I need it to look for a file according to the string returned by player.GetLocation(), so that's no good.

    I really have no idea why it doesn't like it, so any guidance at all would be helpful.

    --------------------------------------------------------------

    Problem Two:

    A few lines later, once my filestream is (or should...) be open, I try to check what the next character is before doing anything about it.

    Code:
    if((MapLoad.peek() == "|") || (MapLoad.peek() == "-") || (MapLoad.peek() == "+"))
    I want it to 'peek' at the next character, and if it is any of those three seen above, execute the code in the if statement without actually getting it. However, I get the error:

    ISO C++ forbids comparison between pointer and integer

    Once for each comparison. I tried get() instead to see if it worked, but no, that didn't work, either. What am I doing wrong? Again, guidance would be appreciated.

    --------------------------------------------------------------

    If anyone could give me a little help (in English if possible, I don't speak C++ as fluently as I'd like to be able to just yet ), that'd be great.

    Thanks in advance!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Crystylla
    Code:
    ifstream MapLoad(player.GetLocation());
    From that line of code (I think that part is all that this particular problem concerns, and the whole program-in-progress is spread over several files anyway), I'm getting an error:

    no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string)'

    player.GetLocation() returns a string, so I was hoping that the above code would try to open a file with the same name as the value returned by player.GetLocation() for writing to.
    The constructor for the file stream only takes a const char pointer argument. There is no overloaded version that accepts a string object. Luckily, string's have a c_str() member function that does just what you need:

    Code:
    ifstream MapLoad(player.GetLocation().c_str());

    Quote Originally Posted by Crystylla
    Code:
    if((MapLoad.peek() == "|") || (MapLoad.peek() == "-") || (MapLoad.peek() == "+"))

    I want it to 'peek' at the next character, and if it is any of those three seen above, execute the code in the if statement without actually getting it. However, I get the error:

    ISO C++ forbids comparison between pointer and integer
    Values in double quotes, i.e. "|", etc..., are string literals and are interpreted in this context in the comparison as the address of said string literal. You are therefore trying to compare the return result/value of the peek function (which returns an int) to the address of a string literal. This is why you get the error about comparing integers and pointers. What you need is this:

    Code:
    if((MapLoad.peek() == '|') || (MapLoad.peek() == '-') || (MapLoad.peek() == '+'))
    Note the use of ' versus ". With ' we are saying we wish to compare the return result of the peek function with the indicated character and not the address of the string.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    UK
    Posts
    2
    Ah, that's perfect! I could kick myself for not being able to figure out that second one, though; I've had a similar problem before.

    Thank you very much!

Popular pages Recent additions subscribe to a feed