Like Tree1Likes
  • 1 Post By hk_mp5kpdw

Fstream problem with file input

This is a discussion on Fstream problem with file input within the C++ Programming forums, part of the General Programming Boards category; Hello. I am currently having a problem reading an input file: It prints an error message stating, "error: invalid operands ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    5

    Fstream problem with file input

    Hello. I am currently having a problem reading an input file: It prints an error message stating, "error: invalid operands of types 'std::ifstream()' and 'int' to binary 'operator>>'".
    The file with the function is below:

    AemRandW.cpp:
    Code:
    #include "AemRandW.h"
    #include <fstream>
    using namespace std;
    
    
    void MapRead(int level, int d, int e, int mapdt){
    ifstream LoadMap();
    int dpos, epos, *dposy = &dpos, *eposy = &epos;
    bool x = 0;
    if(level == 1){ifstream LoadMap("1.aem");}
    if(level == 2){ifstream LoadMap("2.aem");}
    if(level == 3){ifstream LoadMap("3.aem");}
    if(level == 4){ifstream LoadMap("4.aem");}
    if(level == 5){ifstream LoadMap("5.aem");}
    if(level == 6){ifstream LoadMap("6.aem");}
    if(level == 7){ifstream LoadMap("7.aem");}
    if(level == 8){ifstream LoadMap("8.aem");}
    if(level == 9){ifstream LoadMap("9.aem");}
    if(level == 10){ifstream LoadMap("10.aem");}
    while(x == 0){
        LoadMap >> d >> e;
        for(dpos = 0; dpos < d; dpos++){
            for(epos = 0; epos < e; epos++){
            LoadMap >> mapdt[dposy][eposy];
            }
        }
    x = 1;
    }
    }
    AemRandW.h:
    Code:
    #ifndef AEMRANDW_H_INCLUDED
    #define AEMRANDW_H_INCLUDED
    #include <iostream>
    using namespace std;
    
    
    void MapRead(int level, int d, int e, int mapdt);
    
    
    
    
    #endif // AEMRANDW_H_INCLUDED
    The files (.aem) are structured so that the first array dimension (x) is at the beginning, followed by the second (y), followed by the individual array elements. An example ("1.aem") is shown below, with dimension 1's value in green, dimension 2's value in blue and the individual elements in red:

    5 5 1 0 2 1 0 4 1 1 1 0 0 1 5 0 6 6 1 0 3 8 0 4 1 1 7

    Could somebody explain how to avoid this problem please?

    Thanks for your time (and sorry for the waffle!)

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,753
    The problem is probably contained in this line:
    Code:
    ifstream LoadMap();
    You do not need the () when creating an instance of this class.

    Jim

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    5
    Quote Originally Posted by jimblumberg View Post
    The problem is probably contained in this line:
    Code:
    ifstream LoadMap();
    You do not need the () when creating an instance of this class.

    Jim
    Just tried this, works perfectly now. Thanks for the help!

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    You say it works? How does that code work? There is so much wrong with that code... I started typing up a more thorough response but quit because I kept seeing something that had me gobsmacked (and I've seldom had to make use of that word either spoken out loud, written down or even just as a thought in my head).
    rags_to_riches likes this.
    "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

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    Code:
    if(level == 1){ifstream LoadMap("1.aem");}
    ...
    All of these will disappear after the if statement. You are merely "shadowing" your old variable LoadMap, and so you "think" it works.
    What you actually need to do is call open on the object instead to open an appropriate file instead of creating a new object which then disappears.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    5
    Quote Originally Posted by Elysia View Post
    Code:
    if(level == 1){ifstream LoadMap("1.aem");}
    ...
    All of these will disappear after the if statement. You are merely "shadowing" your old variable LoadMap, and so you "think" it works.
    What you actually need to do is call open on the object instead to open an appropriate file instead of creating a new object which then disappears.
    I had discovered that it doesn't work shortly after the comment that was posted by hk_mp5kpdw, though thanks for pointing this out anyway.

    When you say call open on the object, do you mean something like
    Code:
    if(level == 1){LoadMap.open("1.aem");}
    or along these lines? I tried this and when it got to the point of loading the file the program crashed.

    Sorry, you'll have to explain this to me. I sort of feel a bit silly as I was actually doing really well until two days ago when I started file I/O and as I am a bit under-slept at the moment, I am not doing very well learning or doing decent coding.

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Do you see a pattern here?

    Code:
    if(level == 1){ifstream LoadMap("1.aem");}
    if(level == 2){ifstream LoadMap("2.aem");}
    if(level == 3){ifstream LoadMap("3.aem");}
    if(level == 4){ifstream LoadMap("4.aem");}
    if(level == 5){ifstream LoadMap("5.aem");}
    if(level == 6){ifstream LoadMap("6.aem");}
    if(level == 7){ifstream LoadMap("7.aem");}
    if(level == 8){ifstream LoadMap("8.aem");}
    if(level == 9){ifstream LoadMap("9.aem");}
    if(level == 10){ifstream LoadMap("10.aem");}
    Something you could perhaps exploit, say by using stringstream to assemble the file name?

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    5
    Quote Originally Posted by rags_to_riches View Post
    Do you see a pattern here?

    Code:
    if(level == 1){ifstream LoadMap("1.aem");}
    if(level == 2){ifstream LoadMap("2.aem");}
    if(level == 3){ifstream LoadMap("3.aem");}
    if(level == 4){ifstream LoadMap("4.aem");}
    if(level == 5){ifstream LoadMap("5.aem");}
    if(level == 6){ifstream LoadMap("6.aem");}
    if(level == 7){ifstream LoadMap("7.aem");}
    if(level == 8){ifstream LoadMap("8.aem");}
    if(level == 9){ifstream LoadMap("9.aem");}
    if(level == 10){ifstream LoadMap("10.aem");}
    Something you could perhaps exploit, say by using stringstream to assemble the file name?
    Yes I do, and I had tried to find a function that allowed me to select a specific file (which I now know is in stringstream), but couldn't do it or find a suitable function to do it with and decided that (after spending over a few hours of searching) I would just use the rubbish programming attempt (the repeated use of 'if statements').

    Also, I could've got rid of the while loop and 'bool x', as well as freeing pointers "dposy" and "eposy" and adding "LoadMap.close();" to the end, but I am aware that the if statements was the problem.

    I am still a little confused though. Could you (or anyone else) please give me an example of how to implement stringstream into this code? I am still very new to C++ and only started three weeks ago, so I am still inexperienced.
    Last edited by Thaizasaskand; 12-27-2011 at 06:06 PM.

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Code:
    #include <sstream>
    #include <iostream>
    
    using namespace std;
    
    void printStream(const stringstream &ss) {
       cout << "Stringstream as std::string: " << ss.str() << endl;
       cout << "Stringstream as const char *: " << ss.str().c_str() << endl;
    }
    int main() {
    
       stringstream ss;
       for(int i = 0; i < 10; ++i) {
          ss << "Example " << i;
          printStream(ss);
          ss.str(""); // clear it
       }
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file input and file pointer problem
    By drater in forum C Programming
    Replies: 6
    Last Post: 02-18-2008, 09:43 AM
  2. fstream char array input question
    By mcdms in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 09:40 PM
  3. filestreams (fstream) and standard input/output (stdlib)
    By Shadow12345 in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2002, 09:14 AM
  4. file ouput using fstream problem
    By tegwin in forum C++ Programming
    Replies: 4
    Last Post: 03-06-2002, 09:22 AM
  5. fstream input
    By curtner in forum C++ Programming
    Replies: 2
    Last Post: 10-12-2001, 06:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21